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
不是reactjs中未定义错误的函数和绑定_Reactjs_React Native_Redux_Jsx - Fatal编程技术网

不是reactjs中未定义错误的函数和绑定

不是reactjs中未定义错误的函数和绑定,reactjs,react-native,redux,jsx,Reactjs,React Native,Redux,Jsx,获取非函数(或)如果我绑定了未定义错误的绑定,我应该如何绑定这些传入值的函数?我在这里错过了什么。。?需要帮助 子组件: constructor(props) { super(props); this.handler = this.handler.bind(this); this.state = { isBoolFlag: true, } } onClick(e){

获取非函数(或)如果我绑定了未定义错误的绑定,我应该如何绑定这些传入值的函数?我在这里错过了什么。。?需要帮助

子组件:

constructor(props) {
        super(props);
        this.handler = this.handler.bind(this);
           this.state = {
          isBoolFlag: true,
        }
      }

      onClick(e){
         this.setState({
          isBoolFlag: !this.state.isBoolFlag,
       });
        this.props.handler(!this.state.isBoolFlag); //Error here..

      }

      render() {
        return (
          <div>
                 <a onClick={this.onClick.bind(this)}>
          </div>
        );
      }
    }
构造函数(道具){
超级(道具);
this.handler=this.handler.bind(this);
此.state={
是的,
}
}
onClick(e){
这是我的国家({
isBoolFlag:!this.state.isBoolFlag,
});
this.props.handler(!this.state.isBoolFlag);//此处出错。。
}
render(){
返回(
);
}
}
父组件:

构造函数(道具){
超级(道具);
this.handler=this.handler.bind(this);
此.state={
showModule:false,
};
}
{this.state.showModule?:}

您不需要
this.handler=this.handler.bind(this)。函数
处理程序
必须是父函数中定义的函数

此外,请注意:

this.setState({
          isBoolFlag: !this.state.isBoolFlag,
       });
this.props.handler(!this.state.isBoolFlag); //Error here..

使用此代码您不能依赖
isBoolFlag
在调用
处理程序之前的状态被更改,因为setState是异步的。您应该发送一个回调函数作为的第二个参数。

您不需要
this.handler=this.handler.bind(this)。函数
处理程序
必须是父函数中定义的函数

此外,请注意:

this.setState({
          isBoolFlag: !this.state.isBoolFlag,
       });
this.props.handler(!this.state.isBoolFlag); //Error here..

使用此代码您不能依赖
isBoolFlag
在调用
处理程序之前的状态被更改,因为setState是异步的。您应该发送一个回调函数作为的第二个参数。

您需要在setState中使用回调函数,因为setState需要一些时间来改变状态,javascript是异步的,因此
this.state.isBookFlag
仅用于其以前的状态

像这样使用它

this.setState({
          isBoolFlag: !this.state.isBoolFlag,
       }, function(){
      this.props.handler(!this.state.isBoolFlag);
 });
此外,您只需要在父组件中绑定处理程序函数,而不需要在子组件中绑定处理程序函数

编码子组件

constructor(props) {
    super(props);

       this.state = {
      isBoolFlag: true,
    }
  }

  onClick(e){
     this.setState({
          isBoolFlag: !this.state.isBoolFlag,
       }, function(){
      if(this.props.handler !== undefined) {
         this.props.handler(!this.state.isBoolFlag);
      }
     });

  }

  render() {
    return (
      <div>
             <a onClick={this.onClick.bind(this)}>
      </div>
    );
  }
}
构造函数(道具){
超级(道具);
此.state={
是的,
}
}
onClick(e){
这是我的国家({
isBoolFlag:!this.state.isBoolFlag,
},函数(){
if(this.props.handler!==未定义){
this.props.handler(!this.state.isBoolFlag);
}
});
}
render(){
返回(
);
}
}
家长:

constructor(props) {
    super(props);
    this.state = {
      showModule: false,
    };
  }

 <div>
 {this.state.showModule ? <Child2 /> : <Child1 handler={this.handler.bind(this)} />}
            </div>
构造函数(道具){
超级(道具);
此.state={
showModule:false,
};
}
{this.state.showModule?:}

您需要在setState中使用回调函数,因为setState需要一些时间来改变状态,而且javascript是异步的,因此
此.state.isBookFlag
仅用于其以前的状态

像这样使用它

this.setState({
          isBoolFlag: !this.state.isBoolFlag,
       }, function(){
      this.props.handler(!this.state.isBoolFlag);
 });
此外,您只需要在父组件中绑定处理程序函数,而不需要在子组件中绑定处理程序函数

编码子组件

constructor(props) {
    super(props);

       this.state = {
      isBoolFlag: true,
    }
  }

  onClick(e){
     this.setState({
          isBoolFlag: !this.state.isBoolFlag,
       }, function(){
      if(this.props.handler !== undefined) {
         this.props.handler(!this.state.isBoolFlag);
      }
     });

  }

  render() {
    return (
      <div>
             <a onClick={this.onClick.bind(this)}>
      </div>
    );
  }
}
构造函数(道具){
超级(道具);
此.state={
是的,
}
}
onClick(e){
这是我的国家({
isBoolFlag:!this.state.isBoolFlag,
},函数(){
if(this.props.handler!==未定义){
this.props.handler(!this.state.isBoolFlag);
}
});
}
render(){
返回(
);
}
}
家长:

constructor(props) {
    super(props);
    this.state = {
      showModule: false,
    };
  }

 <div>
 {this.state.showModule ? <Child2 /> : <Child1 handler={this.handler.bind(this)} />}
            </div>
构造函数(道具){
超级(道具);
此.state={
showModule:false,
};
}
{this.state.showModule?:}
更新:

我错过了将处理程序传递到组件的过程,就像我为组件传递处理程序一样。

更新:


我错过了将处理程序传递到组件的过程,就像我为组件传递处理程序一样。

谢谢您的回复。我已经在Parent中定义了这个,但是我仍然在this.props中得到错误。处理程序不是函数(…)。感谢您指出,我也刚刚意识到setState不会立即反映。没问题:)您能在问题中添加父级组件代码或至少函数的代码吗?我也在问题中添加了父级组件代码,如果您看不到,请告诉我。。刚刚又更新了问题,谢谢你的回复。我已经在Parent中定义了这个,但是我仍然在this.props中得到错误。处理程序不是函数(…)。感谢您指出,我也刚刚意识到setState不会立即反映。没问题:)您能在问题中添加父级组件代码或至少函数的代码吗?我也在问题中添加了父级组件代码,如果您看不到,请告诉我。。只是再次更新了这个问题是的,我已经在父组件中有了它-但仍然是相同的错误…@monkeyjs您正在根据条件创建子组件,因此在子组件中,您需要在调用它之前检查处理程序函数是否已定义。查看更新的代码是的,我在父组件中已经有了它-但仍然是相同的错误…@monkeyjs您正在根据条件创建子组件,因此在子组件中,您需要在调用它之前检查处理程序函数是否已定义。请参阅更新的代码