I have multiple instances of the same component rendered on the same page. Each of these components have identical behavior and call the same actions. The called actions are asynchronous and wait for the response from an API (after which more actions are created so that the response can be consumed by stores).
If an action is triggered from one of these components, that component needs to handle the eventual response in isolation from its siblings.
For example:
- Three identical buttons are rendered on a page
- Clicking a button will create an action that asynchronously queries an API and dispatches a new action with the response's payload (true)
- Once this response is received, the clicked button needs to turn blue
- The unclicked buttons must be unaffected.
How would I go about implementing this simple example in a React/Flux fashion?
The immediate solution that comes to mind is to assign a unique identifier with the component, pass that ID along with the actions, and listen for that specific ID in the store to indicate that it is time to turn blue.
Another solution is to lift the "color" state in each of the rendered components up to their container and mirror this in the store (i.e., have button1Color, button2Color, ... in the store). The downside is that any addition of a button needs to have its own state variable in a store (more brittle!). Is that the better option?
Is there a better way?
This was also asked in Flux store emitting changes to specific react components rather than all components., but I don't believe there was a clear or satisfying answer.
Edit: Further resources: