1

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?

bebbi
  • 2,489
  • 3
  • 22
  • 36
  • 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. – Or B Jan 09 '17 at 18:04
  • [Updated] I'm doing a fully unparametrized operation on the data (imagine a repeatable operation like decompressing a static string), so the promise won't fail and it is more of a "boot state" that's created. The rest of the state contains already known indices into the not-yet-loaded data. So I'll have to handle this on the component level. Perhaps that is cleaner than the original idea though. – bebbi Jan 09 '17 at 20:58
  • You can create your store after the promise is resolved. and then `ReactDom.render`, or you can have a global 'loading' status to match the Promise resolve status – xiaofan2406 Jan 09 '17 at 23:07
  • #1 sounds like the solution I'm evaluating in the question. – bebbi Jan 10 '17 at 07:34
  • @OB3 Do you want to make it an answer? – bebbi Jan 17 '17 at 07:07
  • @bebbi Sure, added below. – Or B Jan 17 '17 at 08:07

1 Answers1

0

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?

Community
  • 1
  • 1
Or B
  • 1,857
  • 15
  • 22