reactjs – 来自根组件的React / Redux调度操作?
发布时间:2020-12-15 16:18:13 所属栏目:百科 来源:网络整理
导读:我正在尝试使用带有React-Native / Redux的firebase我需要在每次用户登录或注销时发送操作.如何从根组件调度操作? class App extends Component { componentWillMount() { firebase.auth().onAuthStateChanged((user) = { if (user) { // Dispatch Action h
我正在尝试使用带有React-Native / Redux的firebase我需要在每次用户登录或注销时发送操作.如何从根组件调度操作?
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()函数中产生任何副作用.它必须是纯粹的功能. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |