【发布时间】:2016-12-25 03:39:35
【问题描述】:
在 react native 中,如果想让孩子调用父母的函数,我可以这样做:
在父母中:
<Child myFunc={this.handleChildFunc.bind(this)} />
孩子会这样调用该函数:
onpress=this.props.myFunc();
如何在 Navigator 和 NavigatorIOS 中通过 passProps 实现这一点?
<NavigatorIOS
initialRoute={{
component: MyScene,
title: 'My Initial Scene',
passProps: { myProp: this.handleChildFunc.bind(this) } // Not work
passProps: { myProp: ()=>this.handleChildFunc.bind(this) } //Not work
}}
style={{flex: 1}}
/>
更新
感谢您的回复,但我是新来的本机反应,不太了解如何在 navigatorIOS 中实现以下答案。但是,我尝试了以下方法,孩子可以成功调用父母的函数
在 parent 中,将回调作为 props 传递
passProps: { parentFunc: ()=>this.handleFunc() }
在孩子中
this.props.handleFunc();
【问题讨论】:
标签: react-native parent-child navigator