我正在尝试将 firebase 与 React-Native/Redux 结合使用,我需要在每次用户登录或注销时调度一个操作。如何从根组件分派(dispatch)操作?
class App extends Component {
componentWillMount() {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// Dispatch Action here
} else {
// Disptach Action here
}
});
}
render() {
const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
return (
<Provider store={store}>
<Router />
</Provider>
);
}
}
export default App;
最佳答案
class App extends Component {
constructor() {
this.store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
}
componentWillMount() {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.store.dispatch(...);
} else {
this.store.dispatch(...);
}
});
}
render() {
return (
<Provider store={this.store}>
<Router />
</Provider>
);
}
}
export default App;
并且不要在 render()
函数中产生任何副作用。它必须是纯函数。
关于reactjs - React/Redux 从根组件调度操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40699799/