Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过使用道具从另一个组件类提供状态值_Javascript_React Native - Fatal编程技术网

Javascript 通过使用道具从另一个组件类提供状态值

Javascript 通过使用道具从另一个组件类提供状态值,javascript,react-native,Javascript,React Native,我是React Native的新手,我正在尝试制作一些示例应用程序来提高自己。对于这种情况,我只希望当按下按钮时,切换的状态和值会发生变化,例如关闭和打开。并且,打开。现在,我有三个类组件。我不会给应用程序上课。我已经完成了。在本课程中,我添加了一个道具: class A extends Component { constructor(props) { super(props); this.state = {change: true}; }

我是React Native的新手,我正在尝试制作一些示例应用程序来提高自己。对于这种情况,我只希望当按下
按钮时,切换的状态和值会发生变化,例如
关闭
打开
。并且,
打开
。现在,我有三个类组件。我不会给应用程序上课。我已经完成了。在本课程中,我添加了一个道具:

class A extends Component
{
    constructor(props)
    {
      super(props);
      this.state = {change: true};
    }
    render(){ 
     
     return(
      <View>
          <View ...
            <Text>CLOSED</Text>
          </View>
      </View>  
           
    );
  }
}
A类扩展组件
{
建造师(道具)
{
超级(道具);
this.state={change:true};
}
render(){
返回(
{
...
})
}
返回(

当您从组件B调用组件A时,必须按如下方式传递状态:

<A isOpen={this.state.isOpen} />


然后在组件A中,您可以简单地使用props.isOpen访问它。

您可以将isOpen值发送到这样的组件
,然后在组件中,您可以使用
this.props.isOpen
访问值,如果我理解得很好,我意识到,将文本
CLOSED
更改为
OPEN
。我是否要使用另一种方法在A组件中初始化此值?要根据
isOpen
值放置文本,您可以执行
{this.props.isOpen?“OPENED”:“CLOSED”}
是的,这解决了我的问题,谢谢
<A isOpen={this.state.isOpen} />