Example
const actions = this.state.actions.filter((item) => item.key !== key2);
this.setState({ actions, more_action: 'add' });
Solution
this.setState((prevState) => {
const newActions = prevState.actions.filter((item) => item.key !== key2);
return { actions: newActions, more_action: 'add' };
});
Note
prevState is available in setState callback, which is important for avoiding race conditions and ensuring that state updates are based on the latest values.
Top comments (0)