0

I have a component that takes user input. When they save, I use an action to update a store's state

In response to this new information, a results component needs to run an async operation and then respond when new information comes back.

No matter what I try, I seem to be left with a hack setTimeout or a dispatch-within-dispatch error.

The async operation is defined via a datasource and has its own store as outlined here. What's the correct way to structure this behavior?

  1. User input is received by input component.
  2. Input component uses an action to update a store's state
  3. Currently, the store attempts to start the async operation needed by the output component
  4. My choice at this point seems to be Dispatch-with-dispatch error or a hack solution involving setTimeout. Neither feels correct.

What am I missing?

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
  • @JaromadaX So the flow I outlined is 100% how you would structure user input triggering both state change and further async behavior with react and altjs? I have a flow problem, not a code problem. I don't need code. I need program structure answers. – Stefan Kendall Oct 31 '16 at 01:38
  • OK - I guess I've never seen "error or hacks" as part of a flow – Jaromanda X Oct 31 '16 at 02:01
  • @JaromandaX I'll update the question for those who didn't read the full intro text. – Stefan Kendall Oct 31 '16 at 13:35

2 Answers2

0

Ignore the examples about DataSources. Do not use registerAsync in stores.

Instead, use actions to perform async behavior and have results update stores. In the example you gave, do this from the input component:

Action1.updateStore()
AsyncAction2.run()

Then, use a store watching AsyncAction2 actions to update and inform result components.

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
0

If you're sure that you want to be dispatching at that point and just want to avoid getting the "dispatch within a dispatch" error you can use alt's .defer method (see: http://alt.js.org/docs/actions/).

Ben Hare
  • 4,365
  • 5
  • 27
  • 44