React Review and Assessment 2 — Questions and Answers
Question 1: What does the useState hook return?
- An array with the state value and a setter function (Correct answer)
- A single state object
- A promise that resolves to state
- The previous state only
Correct answer: An array with the state value and a setter function
useState returns an array containing the current state value and a function to update it.
Question 2: Which method runs after a class component is first rendered to the DOM?
- componentDidMount (Correct answer)
- componentWillUpdate
- render
- constructor
Correct answer: componentDidMount
componentDidMount fires once immediately after the component mounts to the DOM.
Question 3: What is the purpose of the key prop when rendering lists?
- To help React identify which items changed, were added, or removed (Correct answer)
- To set the CSS styling of each item
- To pass data to child components
- To trigger re-renders manually
Correct answer: To help React identify which items changed, were added, or removed
Keys give list items stable identity so React can efficiently update the DOM.
Question 4: How do you prevent a form's default submit behavior in React?
- Call event.preventDefault() in the handler (Correct answer)
- Return false from render
- Use a div instead of form
- Set submit={false} on the form
Correct answer: Call event.preventDefault() in the handler
Calling event.preventDefault() stops the browser from reloading on submit.
Question 5: What does the useEffect dependency array control?
- When the effect re-runs based on changed values (Correct answer)
- The order of component rendering
- Which props are passed down
- The CSS transition timing
Correct answer: When the effect re-runs based on changed values
The effect re-runs only when a value in the dependency array changes.
Question 6: Which of these is a controlled component?
- An input whose value is driven by React state (Correct answer)
- An input that manages its own DOM value
- A component without props
- A component using refs only
Correct answer: An input whose value is driven by React state
A controlled component's value is bound to state and updated via onChange.
Question 7: What is JSX ultimately compiled to?
- React.createElement calls (Correct answer)
- Plain HTML strings
- CSS rules
- Web Components
Correct answer: React.createElement calls
JSX is syntactic sugar that transpiles into React.createElement function calls.
What does the useState hook return?