Sequential Actions After Events
What you'll learn: How to chain multiple operations directly in an event handler instead of spreading them across state updates and effects.
The Synchronous Chain Problem
When a button click needs to trigger multiple actions — update state, call an API, log analytics, update localStorage — beginners often split these across multiple useEffect hooks, each watching for the previous state change. This creates a fragile chain of asynchronous reactions.
The mistake looks like this:
- User clicks → handler sets
formSubmittedtotrue - Effect watching
formSubmitted→ makes API call, setsapiResponse - Effect watching
apiResponse→ logs to analytics - Another effect → updates localStorage
This waterfall is unnecessary and error-prone.
Do It All in One Place
Event handlers are synchronous by default. You can perform multiple actions sequentially right there:
function handleSubmit(e) {
e.preventDefault();
// Action 1: Update state
setIsSubmitting(true);
// Action 2: Call API
fetch('/api/save', { method: 'POST', body: formData })
.then(response => response.json())
.then(data => {
// Action 3: Update with result
setResult(data);
// Action 4: Log analytics
analytics.track('form_submitted');
// Action 5: Update localStorage
localStorage.setItem('lastSubmit', Date.now());
// Action 6: Final state update
setIsSubmitting(false);
});
}
No effects needed. The handler orchestrates everything in response to the user's action.
Think: Cause and Effect
The user event is the cause. Everything that happens because of that event should happen in response to that event — in the handler. Effects are for synchronizing with external systems, not for creating artificial chains of internal logic.
Key Takeaway: When multiple actions should happen because the user did something, chain them directly in the event handler — don't scatter them across effects watching state changes.