Reputation: 536
I am using bottom tab navigator in React-native for Navigation. When I switches tab, component are not updating. Pls let me know how can I update/refresh whole component when I tap on tab at bottom Tab Navigator
Upvotes: 1
Views: 2544
Reputation: 1866
You can use Navigation listener check Navigation Events, when screen gets focused it will trigger a function like this:
useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
//Your refresh code gets here
});
return () => {
unsubscribe();
};
}, [navigation]);
And class component like this:
componentDidMount() {
this._unsubscribe = navigation.addListener('focus', () => {
//Your refresh code gets here
});
}
componentWillUnmount() {
this._unsubscribe();
}
If you want to force update check this question
Upvotes: 1
Reputation: 6272
Here is a simple solution.
import { useFocusEffect } from '@react-navigation/native';
useFocusEffect(
React.useCallback(() => {
console.log("Function Call on TAb change")
}, [])
);
Here is the link you can read more. https://reactnavigation.org/docs/function-after-focusing-screen/
Upvotes: 5