Custom Hooks in React
15 questions · 12 min · Medium
Test your knowledge of performance optimization in React, including React.memo, useMemo, useCallback, lazy loading, code splitting, virtualization, reconciliation, profiling, and preventing unnecessary re-renders.
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
C. To skip re-rendering a component when its props have not changed according to the comparison
React.memo memoizes a component and can skip rendering it again when its props compare equal. It does not prevent updates caused by the component's own state or consumed context.
A. useMemo
useMemo caches a calculation result and recomputes it when its dependencies change, which can help avoid expensive repeated calculations.
D. To cache a function definition between renders while its dependencies remain unchanged
useCallback returns a cached function reference until one of its dependencies changes. It is useful when function identity matters, such as when passing callbacks to memoized children.
B. React.lazy
React.lazy allows a component to be loaded dynamically. It is commonly combined with Suspense to display a fallback while the component's code loads.
C. Breaking JavaScript into smaller bundles that can be loaded when needed
Code splitting reduces the amount of JavaScript that must be downloaded initially by separating code into smaller chunks that can be loaded on demand.
A. React Profiler
The React Profiler helps analyze rendering activity and measure how much time React spends rendering components.
D. New object and function references may fail shallow prop comparison
Objects and functions created during rendering usually have new references. A memoized child may therefore render again even if the values appear equivalent.
B. To render only the visible portion of a large list and a small surrounding range
Virtualization reduces the number of DOM elements for large lists by rendering the visible items and, commonly, a small overscan range.
C. By helping React identify list items across renders
Stable, unique keys help React match items across renders so that insertions, deletions, and reordering can be handled correctly.
A. Lazy loading components that are not immediately needed
Lazy loading and code splitting can reduce the initial JavaScript payload by postponing the loading of nonessential components.
D. React schedules an update, and the component may render again
Updating state schedules React to process the update. React may render the component again and can skip work in some cases, such as when the new state is identical according to Object.is.
B. Memoization adds complexity and overhead, and the calculation may not be expensive enough to benefit
Memoization is most useful when it avoids meaningful repeated work. Profiling helps determine whether the optimization is worthwhile.
C. To determine when the cached calculation or function should be recomputed or recreated
React compares dependencies with their previous values using Object.is. When a dependency changes, the memoized calculation is recomputed or the cached callback is replaced.
A. Split unrelated context values into separate providers where appropriate
Separating unrelated context values can limit which consumers are notified when a value changes. Context consumers still update when the context they use changes.
D. Measure performance, identify bottlenecks, and apply targeted optimizations
Effective optimization starts with profiling and focuses on actual bottlenecks instead of adding memoization or complexity everywhere.