React practice questions with answers
Practise React questions at your own pace: hooks, state, props, effects, keys, memoisation and rendering. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.
What does useState return?
easy
- The current value only
- An array of the current value and a function to update it
- A promise
- An object with get and set
Answer: B. An array of the current value and a function to update it
Explanation: const [count, setCount] = useState(0). Calling setCount schedules a re-render with the new value.
Why does React need a key prop on items rendered from a list?
easy
- For CSS styling
- To identify each item between renders so React can update, reorder or remove the right element
- To make items clickable
- For accessibility
Answer: B. To identify each item between renders so React can update, reorder or remove the right element
Explanation: Use a stable, unique id. Using the array index as the key causes bugs when items are reordered or inserted.
Which hook runs side effects such as fetching data after render?
easy
- useState
- useEffect
- useMemo
- useRef
Answer: B. useEffect
Explanation: useEffect runs after the render is committed. Its dependency array controls when it runs again; it can return a cleanup function.
How do you pass data from a parent component to a child?
easy
- With props
- With a global variable
- With useEffect
- Children cannot receive data
Answer: A. With props
Explanation: Props flow down. For data many components need, use context or a state library instead of passing props through every level.
When does an effect with an empty dependency array, useEffect(fn, []), run?
medium
- On every render
- Once after the first render (and its cleanup on unmount)
- Never
- Before the first render
Answer: B. Once after the first render (and its cleanup on unmount)
Explanation: In development with StrictMode React runs it twice on mount to help you find missing cleanups, but in production it runs once.
After clicking once, what does the button show?
medium
function Counter() { const [n, setN] = useState(0); return <button onClick={() => { setN(n + 1); setN(n + 1); }}>{n}</button>; }- 0
- 1
- 2
- NaN
Answer: B. 1
Explanation: Both calls use the n captured in this render (0), so both set it to 1. Use the updater form setN(prev => prev + 1) to apply both.
What is the difference between useMemo and useCallback?
medium
- They are the same
- useMemo caches a computed value; useCallback caches a function
- useCallback runs effects
- useMemo is for state
Answer: B. useMemo caches a computed value; useCallback caches a function
Explanation: useCallback(fn, deps) is equivalent to useMemo(() => fn, deps). Both only help when the cached result is passed to something that cares about identity.
What does lifting state up mean?
medium
- Moving state into a global store
- Moving shared state to the closest common parent so siblings stay in sync
- Using useRef instead of useState
- Putting state in the URL
Answer: B. Moving shared state to the closest common parent so siblings stay in sync
Explanation: The parent owns the state and passes it (and its setter) down as props to the children that need it.
Why should you not mutate state directly, as in items.push(x); setItems(items)?
hard
- It is slower
- React compares by reference; the same array means no re-render may happen, and other code sharing that array changes too
- push is not allowed in JSX
- It throws an error
Answer: B. React compares by reference; the same array means no re-render may happen, and other code sharing that array changes too
Explanation: Create a new array: setItems([...items, x]). Immutable updates make changes detectable and predictable.
What is useRef commonly used for? (Select all that apply)
hard
- Getting a reference to a DOM element
- Keeping a mutable value across renders without causing a re-render
- Triggering a re-render when it changes
- Storing a timer or interval id
Answer: A. Getting a reference to a DOM element; B. Keeping a mutable value across renders without causing a re-render; D. Storing a timer or interval id
Explanation: Changing ref.current never re-renders. Use state for values that should update the UI.
What does React reconciliation do?
hard
- Merges CSS files
- Compares the new element tree with the previous one and applies the minimal set of DOM changes
- Re-downloads components
- Validates props
Answer: B. Compares the new element tree with the previous one and applies the minimal set of DOM changes
Explanation: React diffs by element type and key. Different types replace the subtree; matching keys let it reuse and move elements.
A component re-renders often and is slow. Which is usually the right first step?
hard
- Wrap everything in useMemo
- Measure with the React Profiler, then memoize the expensive part (React.memo, useMemo) or move state closer to where it is used
- Switch to class components
- Disable StrictMode
Answer: B. Measure with the React Profiler, then memoize the expensive part (React.memo, useMemo) or move state closer to where it is used
Explanation: Memoization has costs too. Find the real bottleneck first; often moving state down or splitting a component is the cleanest fix.