Javascript 在发送给子组件的回调中找不到父组件的道具

Javascript 在发送给子组件的回调中找不到父组件的道具,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我已经实现了从父组件到子组件的回调,并成功地从子组件调用了它,如本文所述。在我的回调中,我需要父级的props,即navigateprop。但是回调中的this.props返回undefined。如何获取道具对象?我不想把导航道具传给孩子 回拨: _onSeperatorPress(item){ console.log(item); console.log(this); this.navigation.navigate("main"); } 父调用子对象:

我已经实现了从父组件到子组件的回调,并成功地从子组件调用了它,如本文所述。在我的回调中,我需要父级的
props
,即
navigate
prop。但是回调中的
this.props
返回
undefined
。如何获取道具对象?我不想把导航道具传给孩子

回拨:

  _onSeperatorPress(item){
    console.log(item);
    console.log(this);
    this.navigation.navigate("main");
  }
父调用子对象:

    <HorizontalList
      data={genresData}
      keyExtractor={(item, index) => item.description}
      onPress={this._onSeperatorPress} />
item.description}
onPress={this.\u onSeperatorPress}/>
子调用回调:

<TouchableHighlight
            onPress={() => this.props.onPress(item)}
            onShowUnderlay={separators.highlight}
            onHideUnderlay={separators.unhighlight}>
            </TouchableHighlight>
this.props.onPress(项目)}
onShowUnderlay={separators.highlight}
onHideUnderlay={separators.unhighlight}>

您有绑定问题。您需要绑定函数才能访问函数中的状态或道具

ES5:正常函数,您需要在构造函数中手动绑定它

this.handleFunction = this.handleFunction.bind(this);

handleFunction(){
   console.log(this.props); // props are available
}
ES6:Arrow函数-绑定自动进行,您可以避开与范围相关的问题

handleFunction = () = {
  console.log(this.props); // props are available 
}

是什么阻止你共享代码wr道具未定义?@请三思,它与链接答案中所写的完全相同。从你的描述来看,这似乎是一个绑定问题。请检查回调中的
这个
是否是您的组件实例。@是的,它是。如何解决?如果父级在链接代码中传递更改处理程序,请改为:
。基本上,我们是将变更处理程序预绑定到父组件实例。现在,
this
应该是父实例,您应该能够访问道具等。