In response to a state change, I want to trigger another state change. Is that inherently a bad idea?
The specific sort of scenario is that the component is modeled as a state machine that renders different information according to the value of this.state.current_state
. But external events can prompt it to experience a state transition, via changes to it's state through a flux store. Here's a contrived scenario to get the idea across:
I think the correct lifecycle method to do this would be shouldComponentUpdate
. Something to this effect:
shouldComponentUpdate: function(nextProps, nextState) {
if (nextState.counter > 4 && this.state.current_state !== DISPLAY_MANY) {
this.setState({ current_state: DISPLAY_MANY });
}
return true;
}
In some child component, counter
may get incremented, so rather than inferring what it would display based on the value of some counter
variable, I'd like to encode the states explicitly.
The real scenario is more complicated than this, but hopefully this scenario is detailed enough to get the idea across. Is it OK to do what I'm thinking?
EDIT: fixed code example to avoid triggering infinite loop by adding extra state condition