Custom Hooks in React
15 questions · 12 min · Medium
Test your knowledge of state management in ReactJS with 15 multiple-choice questions covering the useState hook, state updates, functional updates, state immutability, lifting state up, sharing state between components, and common state management patterns.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. To manage component state
The useState hook adds state to function components and provides a setter function to request updates to that state.
C. An array containing the current state and a setter function
useState returns an array with the current state value and a setter function, which are commonly extracted using array destructuring.
B. By calling its setter function
The setter function requests a state update and allows React to render the component with the updated state when necessary.
D. React schedules an update and may render the component again
A state setter requests an update. React may batch updates and render the affected component again; if the next state is identical according to Object.is, React can skip rendering work.
C. It calculates the next state from the pending previous state
A functional update such as setCount(previousCount => previousCount + 1) calculates the next value from the pending previous state and helps avoid stale-value issues.
A. Create a new object containing the updated properties
React state should be treated as immutable. Creating a new object, often with the spread syntax, makes the update detectable and preserves the previous state object.
B. Create a new array containing the existing items and the new item
Use a non-mutating operation such as the spread syntax or concat() to create a new array, then pass it to the state setter.
D. To provide the initial state for the component
The initial value establishes the state when the component is initialized. Subsequent renders use the current state rather than resetting it to the initial value.
C. When the initial state is expensive to calculate and should be initialized lazily
Passing a function such as useState(() => calculateInitialValue()) lets React use it as a lazy initializer rather than using its return value from a function call written directly in the argument. In development Strict Mode, React may call the initializer twice to check for impurities.
A. Moving shared state to the closest common parent of the components that need it
Lifting state up allows related components to share a single source of truth by storing the state in their closest common parent and passing values and handlers through props.
D. Each component manages its own independent state
Each component instance has its own state. To synchronize values across components, state can be lifted to a common parent or shared through an appropriate state management mechanism.
B. Managing the value of a controlled form input
A controlled input receives its value from React state and updates that state through an event handler such as onChange.
C. Use functional updater functions when each update depends on the previous result
Updater functions are processed using the pending state in sequence. For example, calling setCount(c => c + 1) three times requests three increments without relying on a potentially stale captured value.
A. useReducer
useReducer centralizes state transition logic in a reducer function and is useful when updates involve multiple related values or complex actions.
D. The current render's state value remains unchanged within that render's code
State values are snapshots for a particular render. Calling a setter requests a future render with updated state; it does not change the value captured by the currently executing code.