0

In react native after componentDidMount() its not call again when you navigate to other screens and return to your component (screen). so react native navigation suggest do it by bellow JS codes:

componentDidMount() {
    this._Reload = this.props.navigation.addListener('focus', () => {
        //do something to reset component
    });
}

componentWillUnmount() {
    this._Reload();
}

but now I get this error: undefined is not an object (evaluating 'this.props.navigation.addListener') because I use my component in a tab component(nested). how can I add a focus to my component to refresh? I think I must do it by prop drilling.

Mehrzad Tejareh
  • 635
  • 5
  • 21

1 Answers1

1

I would suggest passing parent screen navigation object (so that you will have focus listener) via context and then in your tab component set the listener to that parent navigation.

Check this for ref on how to pass data to your tab component : Passing params to tab navigator React Navigation 5

Alternatively for a functional component you can use this hook: useFocusEffect from @react-navigation/native

Vivek_Neel
  • 1,343
  • 1
  • 14
  • 25