【发布时间】:2020-11-02 05:45:46
【问题描述】:
Child.js:
export default class Child extends Component {
testFunc = () => {
console.log("test")
}
componentDidMount() {
this.props.onRef(this)
}
Parent.js:
export default class Parent extends Component {
render() {
return (
<>
<Child onRef = {ref => (this.child=ref)}> </Child>
<Button onPress={this.child.testFunc()}></Button>
</>
)
}
}
我想知道如何在 React Native 中从父组件调用子组件中的函数?我尝试了上面的代码,我收到错误“TypeError undefined is not an object (evalating '_this.child.testFunc')。
当我在这里尝试建议的解决方案时遇到同样的错误:Call child function from parent component in React Native。
有人可以帮帮我吗?
【问题讨论】:
标签: javascript react-native parent-child