【问题标题】:ComponentWillUnmount is called before componentDidmount在 componentDidmount 之前调用 ComponentWillUnmount
【发布时间】:2020-04-15 13:22:24
【问题描述】:

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

我从第 2 页到第 1 页使用 导航.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);
  }

【问题讨论】:

标签: reactjs react-native


【解决方案1】:
this.props.navigation.navigate('PAGE1');
//instead of this use the one below
this.props.navigation.goBack();

导航不卸载PAGE2,所以相关函数没有被调用。

【讨论】:

  • 我尝试将它带到第 0 页,这就是我使用导航的原因
  • goBack() 转到堆栈中的上一个路由。我认为你应该检查你的屏幕和导航。我想声明 componentDidMount 不会调用每个屏幕更改。当我第一次使用它时,我也期待它。你应该看看 react-navigation 的焦点和模糊事件。
猜你喜欢
  • 2017-10-19
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
相关资源
最近更新 更多