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:
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:
I think you're unnecessarily splitting the proper answer to your question in half, and debating which half to use. You should use both.
Your store ought to have a hash of values that correspond to different sub-components's eventual states, hashed on some sort of id.
That store might have a state that looks like;
{
id_1: {
//api response object
},
id_2: {
//api response object
},
...
}
You then need a container whose job is to take the data from the flux store, and parse it to provide it's child components with proper props.
That container should have a function on it that does a lookup to find the correct color for each child, e.g.;
getColor: function(id) {
return this.props.flux_store[id].some_value == 'condition' ? 'red' : 'green';
}
And then that container should render;
<SubComponent color={this.getColor('id_1')} key='id_1' />