[当在react-native-router-flux中使用Actions.reset(“ key”)时,会影响该组件的原因

问题描述 投票:1回答:1

我使用react-native-router-flux作为react-native路由器,在我的情况下,当用户注销时,我使用Action.reset(“ key”),但是getDerivedStateFromProps next props仍然具有-data,谁能告诉我原因,或如何使用其他方式解决问题。

react-native router react-native-router-flux
1个回答
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中看到这一点。

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

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.

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

© www.soinside.com 2019 - 2024. All rights reserved.