如何在react-native中传递child和parent之间的数据? [英] How to pass data between child and parent in react-native?

查看:96
本文介绍了如何在react-native中传递child和parent之间的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

module.exports= class APP extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      key1:'key1',
      key2:'key2'
    };
  render() {
    return (
       <View>
          <Button
             onPress={this.goToTisch}>
             1
          </Button>
          <Button
             onPress={this.goToTisch}>
             2
          </Button>
       </View>
    );
  }
}

我只是用react-native写一个应用程序并做不知道如何从子元素更新父状态。提前谢谢

I just write an app with react-native and do not know how to update the parent state from the child element. thank you in advance

推荐答案

要从子方面调用Parent的方法,您可以像这样传递引用。

To call Parent's method from child, you can pass the reference like this.

父类

<ChildClass
    onRef={ref => (this.parentReference = ref)}
    parentReference = {this.parentMethod.bind(this)}
/>

parentMethod(data) {

}

儿童班

let data = {
    id: 'xyz',
    name: 'zzz',
};

//这将调用父方法

this.props.parentReference(data);

这篇关于如何在react-native中传递child和parent之间的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆