`componentDidMount()` function is not called after navigation

后端 未结 9 1789
旧巷少年郎
旧巷少年郎 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:10
    //na pagina que você quer voltar
    import {NavigationEvents} from 'react-navigation';
    
    async atualizarEstado() {
      this.props.navigation.setParams({
      number: await AsyncStorage.getItem('count'),
    });}
    
    render() {
    return (
      <View style={styles.container}>
        <NavigationEvents onDidFocus={() => this.atualizarEstado()} />
      </View>
    );
    }
    
    0 讨论(0)
  • 2020-12-09 04:11

    According to react-navigation docs we can use as below

    componentDidMount () {
      this.unsubscribe= this.props.navigation.addListener('focus', () => {
        //Will execute when screen is focused
      })
    }
    
    componentWillUnmount () {
      this.unsubscribe()
    }
    

    Similar to vitosorriso`s answer but should changed didFocus to focus according to docs

    0 讨论(0)
  • 2020-12-09 04:13

    React-navigation keeps the component mounted even if you navigate between screens. You can use the component to react to those events :

    <NavigationEvents
      onDidFocus={() => console.log('hello world')}
    />
    

    More info about this component here.

    0 讨论(0)
提交回复
热议问题