React native React Native |从另一个组件获取开关值

React native React Native |从另一个组件获取开关值,react-native,React Native,我想从Map.js中获取ToggleCampus内部开关的值。如何从ToggleCampus.js更新Map.js中的状态值 Map.js export default class Map extends React.Component{ constructor(props) { super(props); this.state = { switchVal: true}; } render(){ return (

我想从Map.js中获取ToggleCampus内部开关的值。如何从ToggleCampus.js更新Map.js中的状态值

Map.js

export default class Map extends React.Component{
    constructor(props) {
        super(props);
        this.state = { switchVal: true};
    }

    render(){
        return (
             <ToggleCampus switchVal = {this.state.switchVal} />   
        );
    }
}
    export default class Map extends React.Component{
        constructor(props) {
            super(props);
            this.state = { switchVal: true};
        }
       changeSwitch = (value) => {
this.setState({switchVal:value});
}

        render(){
            return (
                 <ToggleCampus changeSwitch={this.changeSwitch} switchVal = {this.state.switchVal} />   // passed changeSwitch
            );
        }
    }
导出默认类映射扩展React.Component{
建造师(道具){
超级(道具);
this.state={switchVal:true};
}
render(){
返回(
);
}
}
ToggleCampus.js

export default class ToggleCampus extends React.Component {
    constructor(props){
        super(props);
    }

    render() {
        console.log(this.props.switchVal);
        return(
                <Switch
                    value={this.props.switchVal}
                    *(not sure how to use onChange here)*
                />
        );
    }
}
export default class toggle.Component{
建造师(道具){
超级(道具);
}
render(){
console.log(this.props.switchVal);
返回(
);
}
}

因此,基本上您需要做的是将函数作为道具传递给ToggleCampus以更新switchVal。与在ToggleCampus中一样,您希望更改按钮单击时的值,因此请检查以下方法:

Map.js

export default class Map extends React.Component{
    constructor(props) {
        super(props);
        this.state = { switchVal: true};
    }

    render(){
        return (
             <ToggleCampus switchVal = {this.state.switchVal} />   
        );
    }
}
    export default class Map extends React.Component{
        constructor(props) {
            super(props);
            this.state = { switchVal: true};
        }
       changeSwitch = (value) => {
this.setState({switchVal:value});
}

        render(){
            return (
                 <ToggleCampus changeSwitch={this.changeSwitch} switchVal = {this.state.switchVal} />   // passed changeSwitch
            );
        }
    }
导出默认类映射扩展React.Component{
建造师(道具){
超级(道具);
this.state={switchVal:true};
}
changeSwitch=(值)=>{
this.setState({switchVal:value});
}
render(){
返回(
//传递式转换开关
);
}
}
在togglecampus.js中

export default class ToggleCampus extends React.Component {
    constructor(props){
        super(props);
    }

    render() {
        console.log(this.props.switchVal);
        return(
<>
                <Switch
                    value={this.props.switchVal}
                    *(not sure how to use onChange here)*
                />
<Button title="click" onPress={() => this.props.changeSwitch(false)} /> // added this
           </>     
        );
    }
}
export default class toggle.Component{
建造师(道具){
超级(道具);
}
render(){
console.log(this.props.switchVal);
返回(
this.props.changeSwitch(false)}/>//添加了这个
);
}
}
希望能有帮助