All jobs / Frontend / web developer / React
Frontend / web developer interview question · 0–1 years
What is useEffect for?
The strongest answer
Running side effects after render — fetching data, subscribing, syncing with something outside React — with a cleanup function and a dependency list that says when to run again
Why it works
Right. After render, for things React itself does not do.
What a good answer covers
- Effects run after render.
- Return a cleanup.
- Dependencies decide when it re-runs.
- Not for deriving state.
Answers that fall short
- Close, but not the strongest: Calculating values for rendering
Compute during render; effects are for the outside world. - Weak: Making a function run before render
Effects run after the render is committed. - Red flag: Replacing componentDidMount exactly
Similar but not the same lifecycle.
Who gets asked this
More React questions
- What happens if useEffect has an empty dependency array, and if it has none at all?
- How do you render something only when a condition holds?
- Why does onClick={handleClick()} fire during render instead of on click?
- Two sibling components need the same piece of data. Where does it live?
- Why does a component return a fragment instead of a wrapping div?
- Why does items.push(x); setItems(items) not re-render?
- When is a React piece a reusable component versus a page-level screen?
- In-app navigation full-reloads the page. What did the markup do wrong?