React native Undefined不是react native中的对象(评估_this.ref.method)

React native Undefined不是react native中的对象(评估_this.ref.method),react-native,React Native,我有两个部分: 1:家长 2:孩子 我想把一些数据从孩子传给父母。事实上,我在父组件中有一个方法,如下所示: mymethod= (message) => { console.warn(message); } 在子组件中,我使用以下内容: <Parent ref= { (child) => {this.refs= child; }} /> this.refs.mymethod(message); {this.refs=child;}}/> th

我有两个部分:

1:家长 2:孩子

我想把一些数据从孩子传给父母。事实上,我在父组件中有一个方法,如下所示:

mymethod= (message) => {
console.warn(message);
}
在子组件中,我使用以下内容:

      <Parent ref= { (child) => {this.refs= child; }} />
      this.refs.mymethod(message);
{this.refs=child;}}/>
this.refs.mymethod(消息);
但当我运行该代码时,我得到了以下结果:

Undefined不是对象(评估此.ref.method)


你所做的是错误的。您需要将该方法作为prop从父对象传递给子对象,并在需要时在子对象中调用该方法

  export default Parent extends Component{
         mymethod= (message) => {
    console.warn(message);
    }
     render(){
       return (
       <div>
         <Child mymethod={this.mymethod} />
          <Child2 />
         </div>)
       }    
          }
导出默认父级扩展组件{
mymethod=(消息)=>{
控制台。警告(消息);
}
render(){
返回(
)
}    
}
您可以通过
this.props.mymethod(message)