首页 > 解决方案 > 在 componentDidmount 之前调用 ComponentWillUnmount

问题描述

我有一个名为 PAGE2 的组件,我将从该组件返回到组件 PAGE1。现在,当我使用不同的道具从 PAGE1 移动到 PAGE2 时,我看到 PAGE2 具有旧值(此处未调用包含 fetchListValues 函数的 componentDidMount)

我正在使用 navigation.navigate('PAGE1'); 从第 2 页到第 1 页。

  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);
  }

标签: reactjsreact-native

解决方案


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

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


推荐阅读