React REACT 3 — Questions and Answers
Question 1: What problem does React Context primarily solve?
- Passing data deeply without prop drilling (Correct answer)
- Improving render performance automatically
- Managing async requests
- Replacing the virtual DOM
Correct answer: Passing data deeply without prop drilling
Context lets you share values across the tree without manually passing props at every level.
Question 2: When does a component re-render by default?
- When its state or props change (Correct answer)
- Only on page load
- Every 16 milliseconds
- Only when the user scrolls
Correct answer: When its state or props change
A component re-renders when its own state changes or it receives new props.
Question 3: What does React.memo do to a functional component?
- Skips re-rendering if props are unchanged (Correct answer)
- Caches its internal state forever
- Converts it to a class
- Forces it to re-render every tick
Correct answer: Skips re-rendering if props are unchanged
React.memo performs a shallow prop comparison and skips re-rendering when props are equal.
Question 4: What is the second argument to useEffect?
- A dependency array controlling when it runs (Correct answer)
- A cleanup callback
- The initial state
- A ref object
Correct answer: A dependency array controlling when it runs
The dependency array tells React which values to watch to decide when the effect re-runs.
Question 5: What happens if you call a state setter with the same value as current state?
- React may bail out and skip re-rendering (Correct answer)
- It always forces a re-render
- It throws an error
- It resets all other state
Correct answer: React may bail out and skip re-rendering
If the new state is identical (Object.is) to the current state, React can bail out of re-rendering.
Question 6: Which is the correct way to update state based on the previous state?
- setCount(prev => prev + 1) (Correct answer)
- setCount(count + 1) inside a loop
- count = count + 1
- this.count++
Correct answer: setCount(prev => prev + 1)
Using the functional updater form ensures updates use the latest state value.
Question 7: What does the children prop represent?
- The nested content passed between component tags (Correct answer)
- An array of child components only
- The parent component reference
- The component's internal state
Correct answer: The nested content passed between component tags
The children prop holds whatever JSX is nested inside a component's opening and closing tags.
What problem does React Context primarily solve?