1

In redux store, is it possible to load different set of reducers into one single store? My attempt failed

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

I've also created a miniature to demo the problem.

https://codesandbox.io/s/n5r445nnom

Thian Kian Phin
  • 921
  • 3
  • 13
  • 25
  • Why don't you keep separate reducers for those roles? Or in the same reducer, with different properties? – devserkan Aug 11 '18 at 16:52
  • @devserkan client app and admin app don't share reducers, they are totally different thing, there's no point to load admin reducer when client is logged in, and vice versa. – Thian Kian Phin Aug 11 '18 at 17:12

1 Answers1

2

read this answer very carefully

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

so basically the code in then function runs after the undefined store has been exported and used in other file

to check this you can also add one console.log in then function after store is being set

ashish singh
  • 6,526
  • 2
  • 15
  • 35