在redux商店中,是否可以将一组不同的reducer加载到一个商店中?我的尝试失败了

import userReducers from './reducers'
import adminReducers from './reducers/admin'

//share reducer btw member and admin
let store
getUserRole().then(role => {
   reducers =  role === 'member'
   ? userReducers
   : adminReducers
   console.log('reducers', reducers)
   store =  createStore(
       reducers,
       composeWithDevTools(
           applyMiddleware(thunk)
       )
   )
})

export default store


我还创建了一个缩影演示该问题。

https://codesandbox.io/s/n5r445nnom

最佳答案

仔细阅读此答案

Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

所以基本上then函数中的代码在undefined store导出并在其他文件中使用后运行

要检查这一点,您还可以添加一个console.log,然后在设置存储后运行

09-18 04:22