2016-12-14 102 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。

相关问题