2016-08-18 144 views
3

在反應本地人,如果希望孩子撥打父母的功能,我可以做到以下幾點:陣營原生如何父函數傳遞給孩子

在父:

<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(); 

回答

1

有一個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(二級頁面)。

+0

謝謝您的回答。 – bns

2

此代碼爲我工作

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() 

Check here

+0

感謝您的回答。 – bns

相關問題