阵营原生如何父函数传递给孩子
问题描述:
在反应本地人,如果希望孩子拨打父母的功能,我可以做到以下几点:阵营原生如何父函数传递给孩子
在父:
<Child myFunc={this.handleChildFunc.bind(this)} />
而孩子会这样称呼该功能:
onpress=this.props.myFunc();
如何通过passProps在Navigator和NavigatorIOS中实现此目的?
<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。不过,我尝试以下和孩子可以成功调用父的功能
在母公司,通过回调道具
passProps: { parentFunc:()=>this.handleFunc() }
在孩子
this.props.handleFunc();
答
有一个demo,也许有帮助。该renderScene功能
例:力推新场景的
renderScene(route, navigator) {
let Component = route.component;
return <Component {...route.params} navigator={navigator} />
}
例子:
this.props.navigator.push({
component: Child,
func: customFunc
})
那么你可以使用在儿童页this.props.func
(二级页面)。
答
此代码为我工作
return(
<NavigatorIOS
initialRoute={{
component: MyScene,
title: 'My Initial Scene',
myProp: (() => { this.handleChildFunc.bind(this) }) // work
}}
renderScene={ (route, navigator) =>
<Child navigator={navigator} {...route.passProps} onFun={ route.myProp } />
}
/>
)
当呼叫孩子
this.props.onFun()
+0
感谢您的回答。 – bns
谢谢您的回答。 – bns