Sample questions
Question 1Easy
In a production build, when does useEffect(fn, []) run fn?
- Before every render, including the first one
- Once, after the first render is committed
- Only when the component is about to unmount
- After every render, just like with no array
Answer: B. Once, after the first render is committed
Explanation
An empty dependency array means the effect has no dependencies that can change, so it runs once after mount. In development, StrictMode deliberately runs it twice to surface missing cleanup.
Step-by-step solution
- An empty dependency array tells React the effect has no values that change.
- The effect therefore runs once, after the first render is committed to the screen.
- In a production build it does not run again unless the component remounts.
- So the correct answer is option B.
Why the other options are wrong
- A. Effects run after rendering and commit, not before the first render.
- C. The cleanup runs on unmount, but the effect body itself runs at mount.
- D. With an empty array the effect does not run after every render.
Tip: useEffect(fn, []) runs once after mount; its cleanup runs on unmount.
Question 2Easy
What does useEffect(fn) with no dependency array do?
- Runs fn before the first render
- Never runs fn
- Runs fn after every render
- Runs fn once
Answer: C. Runs fn after every render
Explanation
Without a dependency array, React runs the effect after every committed render.
Step-by-step solution
- The dependency array controls when an effect runs.
- With no array, there is no restriction.
- The effect runs after every render.
Why the other options are wrong
- A. Effects run after render, not before.
- B. It does run.
- D. Running once needs an empty array [].
Tip: No array: every render. Empty array: once.
Question 3Easy
What is logged, in order, when <Demo /> first renders?
import { useEffect } from "react";
function Demo() {
console.log("render");
useEffect(() => {
console.log("effect");
});
return null;
}
- effect
- effect, render
- render
- render, effect
Answer: D. render, effect
Explanation
The component function runs first to produce output. Effects run after React has committed that output.
Step-by-step solution
- The Demo function body runs first and logs "render".
- React commits the returned null output to the screen.
- After the commit, the effect runs and logs "effect".
- So the order is render, then effect, which is option D.
Why the other options are wrong
- A. The effect runs after the render, so "effect" is not logged first.
- B. The order is render first, then effect, not the other way round.
- C. The effect also runs, so "effect" is logged after "render".
Tip: Render code runs first; effects run after React commits the output.
Question 4Easy
When does a useEffect callback with an empty dependency array run?
- After the first render (mount) only
- After every render
- Before the first render
- Only when props change
Answer: A. After the first render (mount) only
Explanation
An empty dependency array tells React there are no reactive values, so the effect runs once after mount.
Step-by-step solution
- No dependencies to track.
- Runs after the first commit only.
Why the other options are wrong
- B. That is the case with no dependency array.
- C. Effects run after the component is committed.
- D. An empty array means no dependencies.
Question 5Medium
When does the cleanup function returned from a useEffect callback run?
- Before the component's very first render
- Before the effect re-runs, and on unmount
- Immediately after the effect function returns
- Only when the whole application is closed
Answer: B. Before the effect re-runs, and on unmount
Explanation
Cleanup undoes the previous effect (unsubscribing, clearing timers) before the next run and on unmount.
Step-by-step solution
- React runs the cleanup returned by an effect before re-running that effect.
- It also runs when the component unmounts.
- So it is not run before first render or just after the effect.
Why the other options are wrong
- A. Nothing precedes the first render.
- C. Cleanup waits until the next run or unmount.
- D. It runs on unmount, not only at app close.
Tip: Cleanup runs before the next effect and on unmount.
Question 6Medium
The component mounts with id=1 and is re-rendered with id=2. What is logged?
function Box({ id }) {
useEffect(() => {
console.log('effect ' + id);
return () => console.log('cleanup ' + id);
}, [id]);
return <h2>{id}</h2>;
}
- effect 1, effect 2, cleanup 1
- effect 1, effect 2
- effect 1, cleanup 1, effect 2
- effect 1, cleanup 2, effect 2
Answer: C. effect 1, cleanup 1, effect 2
Explanation
When a dependency changes, React runs the previous cleanup (with the old value), then the new effect.
Step-by-step solution
- Mount: effect 1.
- id changes: cleanup 1.
- Then effect 2.
Why the other options are wrong
- A. Cleanup of the old effect runs before the new effect.
- B. The old effect is cleaned up first.
- D. The cleanup belongs to the previous id, 1.
Question 7Medium
What is the difference between useLayoutEffect and useEffect?
- useLayoutEffect runs only during server-side rendering
- There is no difference; useLayoutEffect is a legacy alias
- useLayoutEffect runs before paint; useEffect usually after
- useEffect runs before rendering; useLayoutEffect runs after
Answer: C. useLayoutEffect runs before paint; useEffect usually after
Explanation
useLayoutEffect blocks painting, so use it only when you must measure or adjust layout before the user sees the frame.
Step-by-step solution
- UseLayoutEffect runs after the DOM is updated but before the browser paints.
- UseEffect usually runs after the paint, so the user may see the earlier frame first.
- So the correct answer is option C.
Why the other options are wrong
- A. UseLayoutEffect also runs in the browser, not only during server rendering.
- B. The two hooks differ in timing, so they are not aliases.
- D. UseEffect runs after rendering and paint, not before rendering.
Tip: Use useLayoutEffect only for measuring or adjusting layout before paint; use useEffect otherwise.
Question 8Medium
<Parent /> is mounted. What is the order of logs?
function Child() {
useEffect(() => { console.log('child effect'); }, []);
console.log('child render');
return null;
}
function Parent() {
useEffect(() => { console.log('parent effect'); }, []);
console.log('parent render');
return <Child />;
}
- parent render, child render, parent effect, child effect
- parent render, child render, child effect, parent effect
- parent effect, child effect, parent render, child render
- child render, parent render, child effect, parent effect
Answer: B. parent render, child render, child effect, parent effect
Explanation
Renders go parent then child; effects run after commit, child first, then parent.
Step-by-step solution
- Render phase: parent, then child.
- Effects run child first, then parent.
Why the other options are wrong
- A. Child effects run before parent effects.
- C. Effects run after rendering.
- D. The parent renders before its child.
Question 9Hard
The component is mounted and 60 ms pass. What is shown?
function Timer() {
const [n, setN] = useState(0);
useEffect(() => {
const id = setInterval(() => setN(x => x + 1), 10);
return () => clearInterval(id);
}, []);
return <h2>{n > 0 ? 'ticking' : 'idle'}</h2>;
}
- ticking
- idle
- Error
- 0
Answer: A. ticking
Explanation
The functional updater avoids a stale closure, so after some ticks n is above 0 and the text becomes "ticking".
Step-by-step solution
- Interval fires every 10 ms.
- setN(x => x + 1) increments.
- n > 0 gives "ticking".
Why the other options are wrong
- B. The updater function ensures the interval keeps counting.
- C. The interval is cleared on unmount.
- D. The component shows text, not the number.
Question 10Hard
A component fetches data in useEffect when an id prop changes. Fast id changes sometimes show data for an older id. Which fix is standard?
- Remove the dependency array
- Ignore or abort the stale request in the effect cleanup
- Fetch inside the render body instead
- Wrap the fetch in useMemo
Answer: B. Ignore or abort the stale request in the effect cleanup
Explanation
Responses can return out of order; a cleanup that sets an ignore flag or aborts the request prevents stale results from updating state.
Step-by-step solution
- The effect re-runs when id changes.
- Cleanup marks the old request stale.
- Only the latest response sets state.
Why the other options are wrong
- A. That would refetch after every render.
- C. Side effects in render are not allowed.
- D. useMemo does not manage request ordering.
Practise all 20 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →