By default, useEffect runs after the component renders and the changes are painted on the screen.
Click to reveal answer
intermediate
What is the difference between useEffect and useLayoutEffect in terms of execution timing?
useEffect runs after painting to the screen, while useLayoutEffect runs synchronously after DOM mutations but before the browser paints, blocking the paint until it finishes.
Click to reveal answer
beginner
What happens if you pass an empty dependency array [] to useEffect?
The effect runs only once after the first render, similar to componentDidMount in class components.
Click to reveal answer
intermediate
Why should you avoid heavy computations inside useLayoutEffect?
Because useLayoutEffect blocks the browser from painting until it finishes, heavy work can cause UI delays and make the app feel slow.
Click to reveal answer
advanced
How does React schedule effects when multiple state updates happen in one render?
React batches state updates and runs effects after the render completes, ensuring effects run only once per render cycle even if multiple updates occur.
Click to reveal answer
When does useEffect run in React by default?
AAfter the component renders and paints on the screen
BBefore the component renders
CDuring the component render
DBefore the browser paints
✗ Incorrect
useEffect runs after the render and paint, so it does not block the UI.
What does an empty dependency array [] in useEffect mean?
AEffect never runs
BEffect runs after every render
CEffect runs before every render
DEffect runs only once after the first render
✗ Incorrect
An empty array means the effect runs once, like componentDidMount.
Which hook runs synchronously before the browser paints?
AuseEffect
BuseLayoutEffect
CuseState
DuseMemo
✗ Incorrect
useLayoutEffect runs after DOM changes but before paint.
Why avoid heavy work inside useLayoutEffect?
AIt slows down rendering by blocking paint
BIt causes infinite loops
CIt runs too late
DIt does not run on the client
✗ Incorrect
Heavy work blocks paint and delays UI updates.
If multiple state updates happen in one render, when do effects run?
ABefore any render
BAfter each state update separately
CAfter all updates finish and render completes
DDuring the render
✗ Incorrect
React batches updates and runs effects once after render.
Explain when and why you would use useEffect versus useLayoutEffect in React.
Think about when the browser paints and how blocking affects UI.
You got /5 concepts.
Describe how React handles effect execution timing when a component renders multiple times quickly.
Consider how React optimizes multiple updates.
You got /4 concepts.
Practice
(1/5)
1. In React, when does the useEffect hook run its effect function by default?
easy
A. Only when the component unmounts
B. Before the component renders
C. Only once when the component mounts
D. After every render of the component
Solution
Step 1: Understand default useEffect behavior
By default, useEffect runs after every render, including the first render and all updates.
Step 2: Compare with other lifecycle timings
It does not run before render or only once unless dependencies are specified.
Final Answer:
After every render of the component -> Option D
Quick Check:
Default effect runs after render [OK]
Hint: Default useEffect runs after every render unless dependencies given [OK]
Common Mistakes:
Thinking useEffect runs before render
Assuming it runs only once without dependencies
Confusing cleanup timing with effect timing
2. Which of the following is the correct syntax to run a useEffect only once when the component mounts?
easy
A. useEffect(() => { /* effect */ }, [true])
B. useEffect(() => { /* effect */ })
C. useEffect(() => { /* effect */ }, [])
D. useEffect(() => { /* effect */ }, [null])
Solution
Step 1: Identify dependency array usage
An empty array [] means the effect runs only once after the first render (mount).
Step 2: Check other options
Omitting dependencies runs every render; non-empty arrays cause effect to run on changes.
Final Answer:
useEffect(() => { /* effect */ }, []) -> Option C
Quick Check:
Empty dependency array = run once on mount [OK]
Hint: Empty array runs effect once on mount [OK]
Common Mistakes:
Omitting dependency array runs effect every render
A. The effect will run multiple times because dependencies are missing
B. The cleanup function should be a function, not the result of clearInterval
C. setInterval should be replaced with setTimeout
D. The effect will never run because of missing dependencies
Solution
Step 1: Check cleanup function syntax
The cleanup must be a function returning a function, but here it calls clearInterval immediately.
Step 2: Correct cleanup usage
It should return a function: return () => clearInterval(id); to run cleanup on unmount.
Final Answer:
The cleanup function should be a function, not the result of clearInterval -> Option B
Quick Check:
Cleanup must be a function returning a function [OK]
Hint: Cleanup must be a function, not a direct call [OK]
Common Mistakes:
Returning cleanup call instead of function
Confusing dependencies with cleanup
Thinking setInterval needs setTimeout
5. You want a React effect to run only when either propA or stateB changes, but not on every render. Which dependency array is correct?
React.useEffect(() => {
// effect code
}, ???);
hard
A. [propA, stateB]
B. []
C. [propA || stateB]
D. [propA && stateB]
Solution
Step 1: Understand dependency array purpose
The effect runs when any value in the array changes compared to last render.
Step 2: Analyze options
[propA, stateB] lists both dependencies separately, so effect runs if either changes. Options C and D use logical operators, which evaluate to single values and won't track changes properly. [] runs effect only once on mount.
Final Answer:
[propA, stateB] -> Option A
Quick Check:
List all dependencies separately in array [OK]
Hint: List each dependency separately in array to track changes [OK]
Common Mistakes:
Using logical operators inside dependency array
Leaving dependency array empty when dependencies exist
Assuming single combined value tracks all changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.