2016-12-14 112 views
1

當我按下導航欄的右鍵時,我想調用場景組件中的一個函數。React本地路由器Flux - 如何調用場景的方法

在父:

<Router 
     style={routerStyle.router} 
     navigationBarStyle={navBarStyle.navBar} 
     titleStyle={navBarStyle.navTitle} 
     leftButtonStyle={navBarStyle.leftButtonStyle} 
     drawerImage={hamburgerIcon}> 
    <Scene 
     key='History' 
     component={History} 
     title='History' 
     type='reset' 
     rightButtonImage={mailIcon} 
     onRight={this.sendMail} <-- ON THIS 
     rightButtonStyle={navBarStyle.rightButtonStyle} 
     rightButtonIconStyle={navBarStyle.rightButtonIconStyle} /> 
</Router> 

在孩子(History.js):

openModal() { 
    this.setState({modalVisible: true}); <-- DO THIS 
} 
closeModal() { 
    this.setState({modalVisible: false}); 
} 
+1

如何使用Actions.History({modalVisible:true})或Actions.refresh({modalVisible:true}),但是將其設置爲prop。然後,你會觀察componentWillReceiveProps的變化。 –

+0

它的工作原理!特別是我用 'sendMail(){ Actions.refresh(); }'在我的路由器和 'componentWillReceiveProps(){ this.toggleModal(); } toggleModal(){ this.setState({modalVisible:!this.state.modalVisible}); }' 在場景中,請發佈答案,以便我可以接受它:) – Haruspik

回答

2

您可以使用Actions.refresh刷新活動場景的道具。當我使用這個路由器軟件包時,我用它作爲逃生艙口。

請注意,何時以及爲什麼componentWillReceiveProps不會無意中調用toggleModal。

相關問題