1

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

Kumar
  • 436
  • 1
  • 4
  • 16

2 Answers2

4

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/

Engr.Aftab Ufaq
  • 3,356
  • 3
  • 21
  • 47
1

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

Hassan Kandil
  • 1,612
  • 1
  • 13
  • 26