小编Pot*_*neo的帖子

从React Native中的父组件调用子函数

我正在开发我的第一个React Native应用程序.我想要实现的是从父组件执行子函数,这是这样的情况:

儿童

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

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>);
  }
}
Run Code Online (Sandbox Code Playgroud)

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

react-native

28
推荐指数
5
解决办法
3万
查看次数

标签 统计

react-native ×1