Search code examples
javascriptreduxdefault

Redux populate default state from Promise


One of my sub-reducers has a default state that needs to be calculated in a promise.

I could check for an empty state before creating the store, and calculate the default state, then create the store, but that's very brittle as I'd have to know a lot about state internal structure in a place where it doesn't belong.

Is there an good way to do this?


Solution

  • I wouldn't delay the creation of the store, because it does not depend on the data, it is a fundamental part of your app's mechanism (what happens if your promise is rejected?). I suggest dispatching an INIT_STATE action when the promise resolves.

    There's also the option of lazy-loading reducers, please look here: How to dynamically load reducers for code splitting in a Redux application?