Custom Hooks in React
15 questions · 12 min · Medium
Test your knowledge of React component lifecycle concepts and the useEffect Hook with 20 questions covering mounting, updating, unmounting, dependency arrays, cleanup functions, side effects, and effect execution.
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
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
B. To perform side effects in function components
useEffect is used to synchronize a component with external systems and perform side effects such as data fetching, subscriptions, and timers.
C. Mounting
Mounting refers to the initial creation and insertion of a component into the DOM.
B. Updating
A component enters the updating phase when changes to its props or state cause it to render again.
D. Unmounting
Unmounting occurs when React removes a component from the DOM.
C. The effect runs after the initial render and does not re-run due to dependency changes
With an empty dependency array, the effect has no reactive dependencies, so it does not re-run when props or state change.
C. After every completed render
When no dependency array is provided, the effect runs after every completed render.
B. To control when the effect should re-run
The dependency array tells React which reactive values the effect depends on and when the effect needs to synchronize again.
C. Removing subscriptions, timers, or other external resources
Cleanup functions are commonly used to stop timers, unsubscribe from events, disconnect resources, or otherwise undo an effect.
A. useEffect(() => { return () => { /* cleanup */ }; }, []);
An effect can return a function, and React calls that function during cleanup.
B. Before the effect runs again and when the component unmounts
React runs the previous effect's cleanup before re-running that effect after dependencies change and when the component is removed.
A. Fetching data from an API
Data fetching is a common example of an external synchronization task that can be handled with useEffect.
B. Whenever count changes between renders
Because count is listed as a dependency, React re-runs the effect when its value changes between renders.
A. The component enters the updating phase
Changes to state can trigger a new render, which is part of the component's updating lifecycle.
B. To remove the listener when it is no longer needed
Removing the event listener during cleanup prevents stale subscriptions and unnecessary listeners from accumulating.
B. It is a React Hook used in function components
useEffect is a Hook designed for function components to synchronize with external systems and perform side effects.
B. The timer may continue running after it is no longer needed
Timers such as setInterval should generally be cleared during cleanup when the effect no longer needs them.
C. clearInterval()
clearInterval() cancels an interval previously created with setInterval().
B. To help reveal effects that are not properly resilient to setup and cleanup
React Strict Mode can perform an extra setup and cleanup cycle in development to help expose missing or incorrect cleanup logic.
D. Calculating a value directly from existing props and state
Values that can be calculated directly from props and state during rendering generally do not require an effect.
C. Mounting → Updating → Unmounting
A component is typically mounted, may update multiple times as its inputs change, and eventually unmounts.