`componentDidMount()` function is not called after navigation

后端 未结 9 1834
旧巷少年郎
旧巷少年郎 2020-12-09 03:38

I am using stackNavigator for navigating between screens. I am calling two API\'s in componentDidMount() function in my second activity. When i loa

9条回答
  •  春和景丽
    2020-12-09 04:02

    If the upvoted syntax that uses NavigationEvents component is not working, you can try with this:

    // no need to import anything more
    
    // define a separate function to get triggered on focus
    onFocusFunction = () => {
      // do some stuff on every screen focus
    }
    
    // add a focus listener onDidMount
    async componentDidMount () {
      this.focusListener = this.props.navigation.addListener('didFocus', () => {
        this.onFocusFunction()
      })
    }
    
    // and don't forget to remove the listener
    componentWillUnmount () {
      this.focusListener.remove()
    }
    

提交回复
热议问题