How can I run an action when a state changes?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In React, the standard way to run an action after state changes is useEffect. The important detail is that you should use it for real side effects such as network calls, logging, subscriptions, or imperative APIs, not for simple derived values that could be computed during render.
Use useEffect for Side Effects
If you want code to run whenever a specific piece of state changes, put that state in the dependency array.
React runs the effect after the render that reflects the new state value.
Do Not Use an Effect for Derived State
A common mistake is using useEffect just to mirror one state value into another when the result could be calculated directly.
Bad pattern:
Better pattern:
Use an effect only when something external needs to happen because state changed.
Fetch Data When a State Value Changes
A common real use case is refetching when a filter or selected ID changes.
This is exactly the kind of state-driven action useEffect is meant for.
Reacts to Props Too
In React, props changes also trigger rerenders, so the same pattern works when the "state change" you care about actually comes from parent props, context, or other reactive inputs.
The dependency list should match the value that the effect logically depends on, whether that value came from useState, props, or another hook.
Cleanup Matters
If the action creates a subscription, timer, or other persistent resource, return a cleanup function.
Without cleanup, repeated state changes can leak timers, listeners, or stale async work.
Not Every Action Needs an Effect
If the action belongs exactly to the event that changed the state, it can be cleaner to perform it in the event handler itself instead of waiting for an effect. Effects are best when the logic depends on the rendered state value or on coordination with external systems, not when the cause and response already live in the same click or input handler.
Infinite Loops Are the Main Warning Sign
If an effect updates the same state that triggers it, you can create a render-effect-update loop accidentally. When that happens, the real fix is usually to rethink whether the effect should exist at all or whether the update can be moved to the event or data-loading boundary instead.
Common Pitfalls
- Using
useEffectfor values that can be derived directly during render. - Forgetting a dependency and then wondering why the effect does not run on the right state change.
- Adding unnecessary dependencies and causing the effect to run more often than intended.
- Triggering state updates inside an effect in a way that causes accidental loops.
- Forgetting cleanup for subscriptions, timers, or in-flight async work.
Summary
- In React,
useEffectis the normal tool for running actions after state changes. - Use it for side effects, not for simple derived values.
- Put the relevant state or prop in the dependency array.
- Return a cleanup function when the effect allocates persistent resources.
- Most effect bugs come from wrong dependencies or using effects for the wrong job.
Related reading
- How can I run background tasks in React Native?
- How can I run multiple npm scripts in parallel?
- How can I run NodeJS in Docker with MongoDB and RabbitMQ?
- How can I scrape a page with dynamic content created by JavaScript in Python?
- How can I select an element by name with jQuery?
- How can I serve static html from spring boot?
- How can I tell if a DOM element is visible in the current viewport?
- How can I trigger the same function from multiple events with jQuery?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.