If you're writing Swift you can use ViewController's life cycle hook, viewDidAppear or viewWillAppear to know whether specific View is on screen. Is there any way to do the similar thing in ReactNative? looks like no close one in the docs
React Navigation provides a hook that returns a boolean indicating whether the screen is focused or not. The hook will return true when the screen is focused and false when our component is no longer focused. This enables us to render something conditionally based on whether the user is on the screen or not.
There are two approaches to calling an action on screen focusing: Using the withNavigationFocus higher order component provided by react-navigation. Listening to the 'didFocus' event with an event listener.
I don't think react native provides any functions which are called when you pop to previous view(unlike methods like componentWillMount() which is called when component mounts).
But I think you can pass a callback function when you navigate to new scene, and just before popping it call this function.
When you push a new component 'B'
from 'A'
In Component A
callBackPop() {
// manipulate state
}
pushNewScene() {
this.props.navigator.push({name: 'B', callBack: this.callBackPop});
}
In Navigator
renderScene(route, navigator) {
<B navigator={navigator} callback={route.callBack} />
}
In Component B
function popToPrevious() {
this.props.callBack();
this.props.navigator.pop();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With