在componentDidmount之前调用ComponentWillUnmount

时间:2020-04-15 13:22:24

标签: reactjs react-native

我有一个叫做PAGE2的组件,从中我又回到组件PAGE1。现在,当我使用不同的道具从PAGE1移到PAGE2时,我看到的PAGE2带有旧值(此处未调用componentDidMount,其中包含fetchListValues函数)

我使用以下方法从第2页转到第1页 navigation.navigate('PAGE1');

  componentDidMount = () => {
    // in page 2
    this.fetchChats();

    this.backHandler = BackHandler.addEventListener(
      "hardwareBackPress",
      this.handleBackPress
    );
  };

  handleBackPress() {
    console.log('Back pressing', this.props.navigation);
    this.props.navigation.navigate('PAGE1');
    return true;  
  }

   componentWillUnmount() {
    console.log('Hitting component will unmount');
    BackHandler.removeEventListener("hardwareBackPress", 
     this.handleBackPress);
  }

1 个答案:

答案 0 :(得分:0)

this.props.navigation.navigate('PAGE1');
//instead of this use the one below
this.props.navigation.goBack();

正在导航未卸载PAGE2,因此未调用相关函数。

相关问题