React Review and Assessment 3 — Questions and Answers
Question 1: What problem does the useCallback hook solve?
- Memoizing a function so it keeps the same reference between renders (Correct answer)
- Caching API responses automatically
- Replacing useState entirely
- Forcing synchronous rendering
Correct answer: Memoizing a function so it keeps the same reference between renders
useCallback returns a memoized callback that only changes when its dependencies change.
Question 2: How is data passed from a parent to a child component?
- Through props (Correct answer)
- Through state lifting only
- Through the DOM directly
- Through global variables exclusively
Correct answer: Through props
Parents pass data to children via props.
Question 3: What does the Context API help avoid?
- Prop drilling through many intermediate components (Correct answer)
- Component re-rendering entirely
- Using hooks
- Writing JSX
Correct answer: Prop drilling through many intermediate components
Context lets you share values deeply without passing props through every level.
Question 4: Which hook would you use to access a DOM element directly?
- useRef (Correct answer)
- useState
- useMemo
- useReducer
Correct answer: useRef
useRef holds a mutable reference, commonly attached to DOM nodes.
Question 5: What is the correct way to update state based on the previous state?
- Pass a function to the setter: setCount(prev => prev + 1) (Correct answer)
- Mutate the state variable directly
- Reassign with count = count + 1
- Use a global counter
Correct answer: Pass a function to the setter: setCount(prev => prev + 1)
Using the updater function form guarantees you read the latest state value.
Question 6: What does React.Fragment let you do?
- Return multiple elements without adding extra DOM nodes (Correct answer)
- Lazy load components
- Create portals
- Memoize components
Correct answer: Return multiple elements without adding extra DOM nodes
Fragments group children without introducing an extra wrapper element.
Question 7: When does React batch state updates?
- It groups multiple state updates to minimize re-renders (Correct answer)
- Only on page load
- Never, each update re-renders immediately
- Only inside useEffect
Correct answer: It groups multiple state updates to minimize re-renders
React batches updates so several setState calls trigger a single re-render.
What problem does the useCallback hook solve?