【发布时间】:2019-03-16 22:41:16
【问题描述】:
我有一个 React Native 组件 <Parent>,其中包含一个组件 <Child>,我希望能够在 Parent 中调用 Child 的方法之一。在阅读了一堆其他帖子之后,这就是我所拥有的:
Child.js
export default class Child extends Component {
method1() {
console.log("success");
}
render() {
return(
<View/>
)
}
}
家长
export default class Parent extends Component {
render() {
return(
<View>
<TouchableOpacity
onPress={() => this.child.method1()}
/>
<Child
onRef={ref => (this.child = ref)}
/>
</View>
)
}
}
我收到错误“_this2.child.method1 不是函数。”
我尝试过的其他事情是使用refInput 代替onRef,并使用ref => {this.child = ref} 代替ref => (this.child = ref)。
有什么想法吗?
谢谢!
【问题讨论】:
-
任何解决方案
标签: javascript reactjs react-native