Reputation: 138
Ex- I have two components A and B. I need to refresh component A after navigating from component B.
componentDid Mount doesn't work because A is already mounted.
How to achieve this. I am using react navigation to navigate from B->A
Upvotes: 3
Views: 1076
Reputation: 7309
You can either use NavigationEvents
from react-navigation
or can pass a callback which will trigger on navigation.goBack()
.
Check this snack : Temporary Link
Upvotes: 4
Reputation: 1814
You can add a listener for the same in react native.
There are total 4 listners for the same.
Try the below code
componentDidMount(){
const didFocusSubscription = this.props.navigation.addListener(
'didFocus',
payload => {
console.warn('didFocus ', payload);
# just write your code/call a method here which you want to execute when the app comes from a component
this.handleRefresh()
}
);
}
Dont forget to remove it when it is done.
componentWillMount(){
didFocusSubscription.remove();
}
More you can read here. Thanks :)
Upvotes: 2