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

前端之家收集整理的这篇文章主要介绍了react-native – 在React Native中从父组件调用子函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发我的第一个React Native应用程序.我想要实现的是从父组件执行子函数,这是这样的情况:

儿童

  1. export default class Child extends Component {
  2. ...
  3. myfunct: function() {
  4. console.log('Managed!');
  5. }
  6. ...
  7. render(){
  8. return(
  9. <Listview
  10. ...
  11. />
  12. );
  13. }
  14. }

  1. export default class Parent extends Component {
  2. ...
  3. execChildFunct: function() {
  4. ...
  5. //launch child function "myfunct"
  6. ...
  7. //do other stuff
  8. }
  9.  
  10. render(){
  11. return(
  12. <View>
  13. <Button onPress={this.execChildFunct} />
  14. <Child {...this.props} />
  15. </View>);
  16. }
  17. }

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

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

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

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

猜你在找的React相关文章