加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 在React Native中从父组件调用子函数

发布时间:2020-12-15 20:53:15 所属栏目:百科 来源:网络整理
导读:我正在开发我的第一个React Native应用程序.我想要实现的是从父组件执行子函数,这是这样的情况: 儿童 export default class Child extends Component { ... myfunct: function() { console.log('Managed!'); } ... render(){ return( Listview ... / ); }}
我正在开发我的第一个React Native应用程序.我想要实现的是从父组件执行子函数,这是这样的情况:

儿童

export default class Child extends Component {
  ...
  myfunct: function() {
    console.log('Managed!');
  }
  ...
  render(){
    return(
      <Listview
      ...
      />
    );
  }
}

export default class Parent extends Component {
  ...
  execChildFunct: function() {
    ...
    //launch child function "myfunct"
    ...
    //do other stuff
  }

  render(){
    return(
      <View>
        <Button onPress={this.execChildFunct} />
        <Child {...this.props} />
      </View>);
  }
}

在这个例子中,我想记录’Managed!’当我按下父类中的按钮时.它怎么可行?

您可以为子组件添加引用:
<Child ref='child' {...this.props} />

然后像这样调用孩子的方法:

<Button onPress={this.refs.child.myfunc} />

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读