我有一个使用 Redux 框架的 React Native 应用程序,并且我正在使用 Navigator 组件来处理导航。我在导航工作时遇到了一些麻烦,而且我找不到任何关于如何正确操作的好例子,所以我正在寻找一些帮助和澄清。
这是我目前拥有的内容的要点,它正在工作,但我不知道我是否做得对:
根组件
...
renderScene(route, navigator) {
console.log('RENDER SCENE', route);
const Component = route.component;
return (
<Component navigator={navigator} route={route} {...this.props} />
)
}
render() {
return (
<Navigator
renderScene={(route, nav) => this.renderScene(route, nav)}
initialRoute={{ name: 'Signin', component: Signin }} />
)
}
登录组件
...
componentWillReceiveProps(nextProps) {
if (!this.props.signedIn && nextProps.signedIn) {
console.log('PUSHING TO MAIN');
nextProps.navigator.push({ name: 'Main', component: Main });
}
}
问题:
1:我的第一个想法是我应该将navigator.push
代码移动到一个动作中。但是componentWillReceiveProps
,调用该操作的正确位置是什么?当Signin
组件加载时,如果用户已经有一个活动会话,它会触发一个操作来登录用户。默认情况下,他们没有登录,所以当下一个道具通过时,我检查它是否改变,然后推送到Main
.
2:在记录“PUSH TO MAIN”后立即在我的控制台日志中,我看到两个“RENDER SCENE”日志:
[info] 'RENDER SCENE', { name: 'Signin', component: [Function: Signin] } (EXPECTED)
[info] 'PUSHING TO MAIN'
[info] 'RENDER SCENE', { name: 'Signin', component: [Function: Signin] } (WHY?)
[info] 'RENDER SCENE', { name: 'Main', component: [Function: Main] }
Signin
如果我只是推动组件,我很好奇为什么 RENDER SCENE 会被调用两次(第一个是组件) Main
。
最初也是在componentWillReceiveProps
我只检查的方法中:
if (nextProps.signedIn) {
nextProps.navigator.push({ name: 'Main', component: Main });
}
但这导致Main
组件被推送两次。