React Interview Questions
React Interview Questions
In-Depth Answers
1. What are the limitations of React in building large-scale applications?
React is a library focused on the UI layer and lacks built-in solutions for routing, state
management, or form handling, often requiring third-party libraries. Managing deeply
nested components and state (prop drilling) can become cumbersome. Additionally,
performance can degrade if re-renders aren't optimized, and SEO is challenging without
SSR.
2. How does React manage the Virtual DOM, and what are the benefits?
React creates a lightweight copy of the real DOM in memory called the Virtual DOM. When
changes occur, it calculates the difference (diffing) between the current and previous virtual
DOMs and updates only the changed elements in the real DOM. This approach enhances
performance by avoiding unnecessary DOM manipulations.
3. Can React Hooks fully replace Redux for state management?
Hooks like useState, useReducer, and useContext can manage local and some global state,
but Redux is preferable for complex applications that require predictable state
management, middleware, debugging tools, and advanced async handling with thunk or
saga.
4. What are the best practices for managing state in large React applications?
Use local state for UI concerns and Context or Redux for global state. Normalize state shape,
co-locate state logic with components, use memoization, avoid prop drilling, and structure
components cleanly to ensure scalability and maintainability.
5. How would you optimize performance in a React app with large component trees?
Use React.memo, useMemo, and useCallback to prevent unnecessary re-renders. Implement
code-splitting, lazy loading, virtualization (e.g., react-window), and avoid inline functions or
large object creation in JSX. Keep the component tree shallow and performant.
6. Explain React's Strict Mode and its impact on development.
<React.StrictMode> is a development-only tool that highlights potential problems by
double-invoking functions like useEffect, warning about legacy practices, and promoting
best patterns. It helps catch bugs early but has no effect on production builds.
7. How can you prevent unnecessary re-renders in React functional components?
Use React.memo for memoizing components, useCallback for functions, and useMemo for
values. Avoid passing new object/array literals as props and ensure components are pure.
Proper key usage in lists also helps prevent extra renders.
8. Describe the key differences between functional and class components in React.
Functional components are plain functions that accept props and return JSX. With Hooks,
they can now manage state and side effects. Class components use ES6 classes, have
lifecycle methods, and need a render method. Functional components are more concise and
preferred in modern React.
9. What is the significance of the React Fiber architecture?
React Fiber is the underlying engine for rendering in React 16+. It enables incremental
rendering, improved prioritization of updates, interruption of rendering, and better error
handling. This makes UI updates smoother and more responsive.
10. How does React handle side effects, and how can you manage them effectively?
React handles side effects using the useEffect Hook. You can specify dependencies to control
when effects run. Cleanup functions can prevent memory leaks. For async effects, wrap the
logic inside useEffect with async/await and cancel pending operations on unmount.
11. Explain the differences between useMemo() and useCallback() in React.
useMemo memoizes a computed value and recomputes it only when dependencies change.
useCallback memoizes a function and returns the same reference across renders unless
dependencies change. Both are used to optimize performance and avoid unnecessary re-
renders.
12. How would you implement dynamic form handling and validation in React?
Use state or controlled inputs to manage form data dynamically. Libraries like Formik or
React Hook Form simplify form state and validation. Conditional rendering of fields, schema
validation with Yup, and real-time feedback enhance form UX.
13. What is lazy loading in React, and how does it improve application performance?
Lazy loading defers loading non-critical components until they are needed. React provides
React.lazy() and Suspense to implement this. It reduces the initial bundle size and speeds up
the first render, improving performance.
14. How would you handle errors in a React app, and what is the role of error boundaries?
Use Error Boundaries (class components with componentDidCatch) to catch errors in the
component tree and display fallback UIs. For async or hook-based errors, use try-catch
inside hooks or third-party libraries like react-error-boundary.
15. What are the benefits of server-side rendering (SSR) in React applications?
SSR improves initial load time, enables better SEO by rendering content for crawlers,
reduces time-to-interactive, and enhances performance on slow devices. Frameworks like
Next.js simplify SSR in React.
16. How do you handle styling in React components? Discuss different approaches.
You can use plain CSS, CSS Modules, inline styles, styled-components (CSS-in-JS), Tailwind
CSS, or Emotion. CSS Modules and styled-components allow scoped styling, while Tailwind
offers utility-first design. The choice depends on team preference and project needs.
17. How would you pass data between sibling components in React without using Redux?
Lift state up to the nearest common parent and pass callbacks down to children.
Alternatively, use the Context API to share state across the tree without prop drilling.
18. Explain the use case of useEffect() for fetching data from an API.
useEffect can be used to fetch data on component mount or when dependencies change. Use
async/await inside it, handle errors properly, and clean up using an abort controller or a
flag to prevent memory leaks on unmount.
19. How do you handle asynchronous operations in React using async/await or Promises?
Wrap async code inside useEffect or callbacks. Use try-catch for error handling, and ensure
state updates only if the component is still mounted. Debounce or throttle requests to avoid
excessive API calls.
20. How would you re-render a component when the window is resized?
Attach a resize event listener inside useEffect, update state with new dimensions, and clean
up the listener in the cleanup function.
21. Describe how React Context API can be used for state management in an app.
Create a Context object, wrap the app or component tree with a Provider, and consume the
context in any child using useContext. It's useful for themes, auth, or other global data
without prop drilling.
22. What is the role of React Router, and how does it work with dynamic routing?
React Router allows client-side navigation in SPAs. Components like <Route>, <Link>, and
<Switch> define and render views based on the URL. Dynamic routing with params enables
rendering components based on IDs or slugs.
23. Explain the concept of controlled and uncontrolled components in React.
Controlled components use state to manage input values, while uncontrolled components
use refs to access DOM values. Controlled components provide more control and validation,
while uncontrolled are simpler for basic forms.
24. How would you optimize React app performance when handling large lists or grids?
Use windowing or virtualization with react-window or react-virtualized. Paginate or lazy
load list items. Avoid unnecessary re-renders by memoizing rows/components and using
unique keys.
25. Explain the difference between shallow and deep comparison in React's
shouldComponentUpdate.
Shallow comparison checks primitive values or object references, not nested properties.
Deep comparison checks nested structures, but is expensive. shouldComponentUpdate and
React.memo use shallow comparison by default.
26. How do you handle asynchronous code execution and state updates in React?
Use useEffect with async functions. Handle race conditions with AbortController. Avoid
setting state after unmount by checking component's mounted status or using cleanup
functions.
27. How would you implement custom hooks to abstract logic in React?
Custom hooks are reusable functions starting with use. Move repetitive logic like form
handling, timers, or API fetching into them. Return state and handlers to consuming
components. Example: useInput, useFetch.
28. What are higher-order components (HOCs) in React, and how are they used?
HOCs are functions that take a component and return an enhanced component. Used for
logic reuse (e.g., authentication, theming). They wrap components and add props,
behaviors, or logic.
29. How would you implement a search feature with debouncing in React?
Use setTimeout inside useEffect to delay search execution. Clear timeout on each render to
debounce.
30. Explain React's reconciliation process and how it updates the DOM efficiently.
React compares the current Virtual DOM with the previous one using a diffing algorithm. It
tracks element types and keys to detect changes and updates only the changed elements in
the real DOM, reducing expensive reflows and improving performance.