I have child component with an HTML element that triggers a callback from it's parent function on click. The callback then changes the state in the parent component. The changed state is then sent back down to the parent which is used in the conditions of the if statements below. toggle
, a prop from the parent, then dispatches an action that changes a value in the store. However, this dispatch ends up changing the store, and thus one of the props in the parent component, which is also passed down to the child component.
As a result, when I insert this in to my code, I get an infinite loop.
/* no state changes to the current component */
componentWillUpdate(nextProps) {
const {
// ... some props
toggle, // func
} = nextProps;
if (/* case 1 of 2*/)
toggle(true);
} else (/* case 2 of 2 */) {
toggle(false);
}
}
Because the onClick
triggers a callback that changes a field in the parent's state that the if statement above relies on, I have to throw the if statement in componentWillUpdate
(or some React API where I can execute the if statement once I'm sure the change to the parent's state is complete (as opposed to putting it with the onClick
function call).
I want to trigger the above only once after an on-click. I don't want it to loop infinitely. How can I do this?