我正在开发我的第一个 React Native 应用程序。我想要实现的是从父组件执行子函数,情况如下:
Child
export default class Child extends Component {
...
myfunct: function() {
console.log('Managed!');
}
...
render(){
return(
<Listview
...
/>
);
}
}
Parent
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!'
当我按下父类中的按钮时。怎样才算可行呢?
Nader Dabit 的答案已经过时,因为在 ref 属性中使用字符串文字已被弃用 https://facebook.github.io/react/docs/refs-and-the-dom.html#legacy-api-string-refs。截至 2017 年 9 月,我们将这样做:
<Child ref={child => {this.child = child}} {...this.props} />
<Button onPress={this.child.myfunc} />
相同的功能,但我们不使用字符串来引用组件,而是将其存储在全局变量中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)