Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何通过回调检测导航器弹出_Reactjs_React Native - Fatal编程技术网

Reactjs 如何通过回调检测导航器弹出

Reactjs 如何通过回调检测导航器弹出,reactjs,react-native,Reactjs,React Native,我目前正在使用React Native处理一些问题。这是一个场景:我有一个带有可点击页面列表的页面A,当我点击一个页面,比如说页面B时,我可以setState({page:'B'})在页面A中,将当前导航器推到页面B,当我将导航器弹出到页面A时,我想setState({page:'})在页面A中,我目前没有办法检测到路由已经弹出到页面A的背面,因此我不知道在哪里放置setState代码。是否有我缺少的方法?您可以将一个函数传递到路由组件中,该组件将在componentWillUnmount中调用

我目前正在使用React Native处理一些问题。这是一个场景:我有一个带有可点击页面列表的页面A,当我点击一个页面,比如说页面B时,我可以
setState({page:'B'})
在页面A中,将当前导航器推到页面B,当我将导航器弹出到页面A时,我想
setState({page:'})
在页面A中,我目前没有办法检测到路由已经弹出到页面A的背面,因此我不知道在哪里放置setState代码。是否有我缺少的方法?

您可以将一个函数传递到路由组件中,该组件将在
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()
}

您可以将一个函数传递到路由组件中,该组件将在
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()
}