Thian Kian Phin
Thian Kian Phin

Reputation: 931

store not is defined when createStore is execute in async function?

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

Upvotes: 1

Views: 188

Answers (1)

ashish singh
ashish singh

Reputation: 6914

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

Upvotes: 2

Related Questions