在 react-native-router-flux 中使用 Actions.reset( "key" ) 时,什么会影响组件



我使用 react-native-router-flux作为 react-native router,在我的情况下,当用户注销时,我使用 Action.reset("key"(,但getDerivedStateFromPropsnext props仍然有预数据,谁能告诉我原因,或者如何使用另一种方式解决问题。

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

首先我们看到源码js代码,它位于导航商店

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重置操作会擦除整个导航状态,并将其替换为多个操作的结果。 所以它只会改变导航状态。 我们可以在 React-navigation API 中看到这一点。

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

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.

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

最新更新