我找到了解决这个问题的方法。不应该是最好的方式,但它的工作原理。
将您对根组件(App.js)设置loggedIn的身份验证请求设置为true或false。 (在下面的例子中,我使用了firebase身份验证)
完成后,您将状态作为通道传递给Component(InitialSetUp.js),然后您可以使用Redux设置状态。
在InitialSetUp上,您的行为像根,呈现应用程序本身。
以下你可以找到这个例子。
App.js
...
const store = createStore(reducers,
window.__REDUX_DEVTOOLS_EXTENSION__ &&
window.__REDUX_DEVTOOLS_EXTENSION__());
export default class App extends Component {
state = { loggedIn: null};
componentWillMount() {
firebase.initializeApp({
...YOUR SETUP
});
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({ loggedIn: true});
} else {
this.setState({ loggedIn: false});
}
});
}
renderInitialView() {
return <InitialSetUp loggedIn={this.state.loggedIn}/>
}
render() {
return (
<Provider store={store}>
<View style={styles.container}>
{this.renderInitialView()}
</View>
</Provider>
);
}
}
InitialSetup.js
...
class initialSetUp extends Component {
renderInitialView() {
this.props.setLogged(this.props.loggedIn);
return <Navigation />
}
render() {
return (
<View style={styles.container}>
{this.renderInitialView()}
</View>
);
}
}
export default connect(null, actions)(initialSetUp);
好运O/
最好不要在组件构造器中创建存储,而要使用React-Redux中的Provider。在组件内部创建存储不是Redux方式。 – DDS
@DDS这是主要的应用程序组件,因此在构造函数或外部组件中初始化存储没有区别。 – farwayer