Advanced React Interview Questions
When interviewing for senior frontend or React developer roles, you will be expected to deeply understand React's internal workings, performance optimization techniques, and modern concurrent features. Below is a curated list of advanced questions to help you prepare.
1. Deep Dive into the Virtual DOM and Reconciliation
Q: Explain how React's Virtual DOM works and why it's beneficial. A: The Virtual DOM (VDOM) is a lightweight, in-memory representation of the actual DOM. When a component's state or props change, React creates a new VDOM tree.
- React then compares this new tree with the previous VDOM tree using a process called diffing.
- It calculates the most efficient way to update the actual DOM to match the new VDOM. This minimum set of operations is called reconciliation.
- Benefit: Direct DOM manipulation is slow. By batching updates and calculating the minimal changes in memory first, React significantly optimizes rendering performance.
Q: What is the "Fiber" architecture in React? A: React Fiber is a complete rewrite of React's core reconciliation algorithm, introduced in React 16.
- Its primary goal is to make rendering interruptible.
- Previously, React rendered synchronously (the "Stack Reconciler"), meaning a deep component tree could block the main thread, causing dropped frames or unresponsiveness.
- Fiber breaks rendering work into units called "fibers." It can pause, resume, or abort rendering work based on priority (e.g., user input has higher priority than background data fetching).
Q: Why is the key prop important when rendering lists, and what happens if you use the array index as the key?
A: The key prop helps React identify which items have changed, are added, or are removed during the diffing process.
- It allows React to reuse existing DOM elements instead of destroying and recreating them.
- If you use the array index as the key and the list order changes (e.g., sorting, adding/removing items from the middle), React might incorrectly match old elements with new data, leading to incorrect state mapping, unexpected re-renders, and performance degradation. Keys must be unique and stable (e.g., database IDs).
2. Concurrency and Modern Features
Q: What are React Concurrent Features, and how do they improve user experience? A: Concurrent React (fully realized in React 18) allows React to prepare multiple versions of the UI at the same time.
- It makes rendering interruptible, ensuring the UI remains responsive even during heavy rendering tasks.
- Key features include
startTransitionanduseDeferredValue. They allow developers to mark certain state updates as non-urgent, keeping the app responsive to high-priority interactions like typing or clicking.
Q: Explain the purpose of useTransition.
A: useTransition allows you to update the state without blocking the UI.
- It returns
[isPending, startTransition]. - You wrap non-urgent, heavy updates inside
startTransition. - While the heavy update is processed in the background,
isPendingistrue, allowing you to show a loading indicator, but the UI remains interactive for other urgent tasks.
Q: How does Suspense work, and how does it relate to concurrent rendering?
A: Suspense lets you declarative specify a loading state (fallback) for a part of the component tree that is waiting on some asynchronous operation (like lazy-loaded code or data fetching).
- With Concurrent React, Suspense integrates deeply with data fetching libraries (like Relay or React Query), allowing React to pause rendering a component while it waits for data and cleanly transition to the finished state without jarring UI jumps.
3. Performance Optimization
Q: When should you use React.memo, and what are its potential downsides?
A: React.memo is a higher-order component that memoizes the rendered output of a component. It prevents unnecessary re-renders if the props haven't changed (using a shallow comparison).
- When to use: For pure components that render often with the same props, or heavy components where rendering is expensive.
- Downsides: Memoization has a memory and computation cost. Overusing it on simple components can actually decrease performance due to the overhead of the shallow comparison on every render.
Q: How do useMemo and useCallback help prevent unnecessary re-renders?
A:
useMemocaches the result of an expensive calculation. It only recalculates when its dependencies change.useCallbackcaches a function definition.- They are most useful when passing objects or functions as props to child components wrapped in
React.memo. Without them, a new object/function reference is created on every parent render, causing the child to re-render even if the actual data/logic hasn't changed.
Q: What is a memory leak in React, and how do you prevent it?
A: A memory leak occurs when a component is unmounted, but asynchronous operations (like setInterval, event listeners, or unresolved promises) continue to hold references to it, preventing garbage collection.
- Prevention: Always clean up side effects in the return function of
useEffect. UseAbortControllerto cancel pendingfetchrequests if the component unmounts.