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