在react-native-router-flux中使用Actions.reset(“ key”)时,将影响组件的因素

时间:2019-11-28 14:28:24

标签: react-native router react-native-router-flux

我使用react-native-router-flux作为react-native路由器,在我的情况下,当用户注销时,我使用Action.reset(“ key”),但是getDerivedStateFromProps {{1 }}尚有预先数据,谁能告诉我原因,或如何使用其他方法解决问题。

1 个答案:

答案 0 :(得分:1)

react-native-router-flux基于react-navigaion。所以我们可以找到概念和解释。

首先我们看到源js代码,它位于navigationStore中

reset = (routeName, data) => {
    const params = filterParam(data);
    const parent = getParent(this.state, routeName);
    this.dispatch(
      StackActions.reset({
        index: 0,
        key: parent ? parent.key : null,
        actions: [
          NavigationActions.navigate({
            routeName,
            params,
          }),
        ],
      }),
    );
  };
}

StackActions.reset重置操作会擦除整个导航状态,并将其替换为多个操作的结果。因此它只会更改导航状态。我们可以在反应导航api中看到这一点。

并且对于导航生命周期,与Web不同,导航不会重新安装组件。它管理路由器使用堆栈。以下是官方的说法:

Consider a stack navigator with screens A and B. After navigating to A, its componentDidMount is called. When pushing B, its componentDidMount is also called, but A remains mounted on the stack and its componentWillUnmount is therefore not called.

When going back from B to A, componentWillUnmount of B is called, but componentDidMount of A is not because A remained mounted the whole time.

因此,当从路由器堆栈中卸下组件(如返回,重置)时,导航将卸载该组件