React Interview Questions – Advanced
Here are 20 advanced-level React interview questions, designed to challenge your understanding of React concepts and best practices.
1. What are React Fiber and its benefits?
React Fiber is a complete rewrite of React’s reconciliation algorithm, introduced in React 16. It enables incremental rendering of the virtual DOM, breaking rendering work into chunks that can be paused and resumed. Benefits include:
- Improved performance for large applications.
- Support for features like Suspense and Concurrent Rendering.
- Better handling of animations and gestures by prioritizing updates.
2. Explain concurrent rendering in React.
Concurrent rendering is a React feature that allows the rendering process to be interrupted, paused, or abandoned to ensure smoother user experiences. It prioritizes high-priority tasks like user input or animations over less critical updates, improving perceived performance.
3. How does React Suspense work internally?
React Suspense allows components to “wait” for asynchronous data before rendering. Internally, it uses a Promise-like mechanism. When a Promise is thrown during rendering, React pauses rendering and waits until the promise resolves. Suspense boundaries handle the fallback UI until the data is ready.
4. What are React hooks rules, and why are they important?
Hooks rules ensure that hooks are predictable and function as intended. They are:
- Only call hooks at the top level of a function, ensuring consistent execution order.
- Only call hooks within React functional components or custom hooks, ensuring React can track state and effects.
5. What is the difference between controlled and uncontrolled forms, and when would you use each?
Controlled Forms: Managed entirely by React state. They provide validation and dynamic control but can become cumbersome for complex forms.
Uncontrolled Forms: Use the DOM to manage form data with refs for occasional access. They are simpler but offer less control over input values.
Use controlled forms for complex validation and real-time feedback, and uncontrolled forms for basic forms with minimal interactivity.
6. How do React error boundaries work?
Error boundaries are React components that catch JavaScript errors in their child components’ tree, logging errors and displaying fallback UIs. Implemented with lifecycle methods:
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
return { hasError: true };
}
componentDidCatch(error, info) {
console.log(error, info);
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
7. What are render props, and how are they different from HOCs?
Render props and higher-order components (HOCs) both enable component logic reuse:
- Render Props: A function passed as a prop that controls what to render. It allows more explicit and flexible composition.
- HOCs: A function that wraps a component and enhances it with additional props or functionality.
Render props are preferred when you need fine-grained control over rendered output, while HOCs are better for adding generic functionality.
8. How does React handle reconciliation for large lists?
React optimizes reconciliation for large lists using keys. Keys uniquely identify elements, enabling React to minimize DOM operations by:
- Reordering existing DOM nodes instead of recreating them.
- Skipping unnecessary updates for unchanged nodes.
For very large lists, libraries like react-window
or react-virtualized
implement virtual scrolling to improve performance.
9. What are lazy loading and code splitting in React?
Lazy loading delays loading components or resources until they are needed, improving initial load times. Code splitting breaks the application into smaller chunks:
const LazyComponent = React.lazy(() => import('./LazyComponent'));
function App() {
return (
<React.Suspense fallback=<div>Loading...</div>>
<LazyComponent />
</React.Suspense>
);
}
10. How does React.memo improve performance?
React.memo is a higher-order component that prevents unnecessary re-renders of functional components by memoizing their output based on props comparison. Use it when components rely on the same props and are costly to render.
11. Explain the useImperativeHandle hook.
The useImperativeHandle hook customizes the ref instance value exposed to parent components. It is used to expose imperative methods from a child component.
const FancyInput = React.forwardRef((props, ref) => {
const inputRef = useRef();
useImperativeHandle(ref, () => ({
focus: () => inputRef.current.focus(),
}));
return <input ref={inputRef} type="text" />;
});
12. What is the difference between React.forwardRef and useRef?
useRef: Creates a mutable ref object within a component.
React.forwardRef: Passes refs from parent to child components, enabling the parent to control the child’s DOM node or instance.
13. How do you handle deeply nested state in React?
For deeply nested state, consider using:
- The Context API for shared state.
- Libraries like Zustand or Redux for structured state management.
14. How does React Profiler work?
The React Profiler measures the performance of rendering components. It collects render times and updates, helping identify bottlenecks. Add the Profiler component:
<Profiler id="MyApp" onRender={callback}>
<App />
</Profiler>
15. How do you optimize rendering for context updates?
To optimize rendering, split the context into smaller contexts, reducing unnecessary re-renders for unrelated parts of the tree.
16. What is React’s Concurrent Mode?
Concurrent Mode allows React to render non-blocking updates by prioritizing high-priority tasks like animations and user inputs.
17. What is the difference between client-side and server-side rendering?
- Client-Side Rendering (CSR): Renders the app on the client after JavaScript loads, providing faster interactions but slower initial loads.
- Server-Side Rendering (SSR): Pre-renders HTML on the server, reducing initial load times and improving SEO.
18. What are React hydration issues, and how do you debug them?
Hydration issues occur when React’s initial render differs from the server-rendered HTML. Debug by:
- Ensuring data consistency between server and client.
- Using React Developer Tools to inspect hydration warnings.
19. How do you implement an error boundary?
An error boundary is a class component that catches JavaScript errors and displays a fallback UI without crashing the app tree. Use lifecycle methods like getDerivedStateFromError
.
20. Explain React’s architecture and its advantages.
React’s component-based architecture promotes modular, reusable, and testable code. Advantages include:
- Declarative UI design with a virtual DOM for optimized updates.
- Support for modern JavaScript syntax and tools.
- Flexibility to integrate with other libraries and frameworks.