如何使用回调检测导航器弹出窗口

时间:2016-06-16 11:49:32

标签: reactjs react-native

我目前正在使用React Native处理一些问题。这就是场景:我的页面A包含可点击页面列表,当我点击页面时,让我们说出页面B,我可以在页面A中setState({page: 'B'})并将当前导航器推送到页面B,当我将导航器弹回到页面A时,我想在页面A中setState({page: ''})。但是,我目前无法检测到该路线已经弹回到页面A,因此我没有'知道在哪里放置setState代码。我有什么方法可以丢失吗?

1 个答案:

答案 0 :(得分:3)

您可以将函数传递到将在componentWillUnmount中调用的路由组件。

// in your list component, this really depends on how you set up your
// Navigator to handle props, hopefully this will give you an idea of how it could work

this.props.navigator.push(someRoute, {onUnmount: () => this.setState({page: ''})}

// then, in the routed component

componentWillUnmount() {
  this.props.onUnmount()
}
相关问题