Navigation

GyaanSetu Educational Platform

Top 20 React Interview Questions for 2026

Master the core concepts of React, from Hooks and Virtual DOM to advanced state management and optimization.

Hooks & ContextVirtual DOMRedux & StateOptimization
1

How does the Virtual DOM work?

The Virtual DOM is a lightweight copy of the real DOM. When state changes, React updates the Virtual DOM first. It then compares this updated Virtual DOM with the previous version (a process called 'diffing'). Once it identifies the differences, React efficiently updates only the changed elements in the real DOM (reconciliation). This minimizes direct manipulation of the slow real DOM, improving performance.

React / JSX
1// Conceptual representation
2const virtualDOM = {
3 type: 'div',
4 props: { id: 'app' },
5 children: [
6 { type: 'h1', children: 'Hello World' }
7 ]
8};
9
10// React diffs this object against the previous one
11// and patches the real DOM only where necessary.
2

Explain React Hooks: useState, useEffect, useMemo, useCallback.

Hooks let you use state and other React features in functional components. 'useState' manages local state. 'useEffect' handles side effects (fetching data, subscriptions). 'useMemo' memoizes expensive calculations to avoid re-computation on every render. 'useCallback' memoizes functions to prevent them from being recreated on every render, useful for passing callbacks to optimized child components.

React / JSX
1import React, { useState, useEffect, useMemo, useCallback } from 'react';
2
3function ExampleComponent({ list }) {
4 const [count, setCount] = useState(0);
5
6 // Side effect
7 useEffect(() => {
8 document.title = `Count: ${count}`;
9 }, [count]);
10
11 // Memoized value
12 const sortedList = useMemo(() => {
13 return list.sort((a, b) => a - b);
14 }, [list]);
15
16 // Memoized callback
17 const increment = useCallback(() => {
18 setCount(prev => prev + 1);
19 }, []);
20
21 return <button onClick={increment}>Count: {count}</button>;
22}
3

What is prop drilling and how do you avoid it?

Prop drilling occurs when data is passed from a parent component down to a deeply nested child through multiple intermediate components that don't need the data themselves. It makes code hard to maintain. It can be avoided by using the Context API for global state, or state management libraries like Redux or Zustand. Component composition (passing components as children or props) is another effective solution.

React / JSX
1// Prop Drilling
2<GrandParent user={user} />
3 <Parent user={user} /> // Doesn't use user
4 <Child user={user} />
5
6// Avoiding it (Context)
7const UserContext = createContext();
8
9<UserContext.Provider value={user}>
10 <GrandParent />
11</UserContext.Provider>
12
13// In Child:
14const user = useContext(UserContext);
4

What are controlled vs uncontrolled components?

In controlled components, form data is handled by React state; the input's value is controlled by React. In uncontrolled components, form data is handled by the DOM itself using refs. Controlled components offer more flexible data manipulation and validation, while uncontrolled components can be simpler for integrating with non-React code or simple forms.

React / JSX
1// Controlled
2function ControlledInput() {
3 const [val, setVal] = useState("");
4 return <input value={val} onChange={e => setVal(e.target.value)} />;
5}
6
7// Uncontrolled
8function UncontrolledInput() {
9 const inputRef = useRef(null);
10 const handleSubmit = () => alert(inputRef.current.value);
11 return (
12 <>
13 <input ref={inputRef} />
14 <button onClick={handleSubmit}>Submit</button>
15 </>
16 );
17}
5

Explain Context API and when you would use it.

The Context API provides a way to share values like these between components without having to explicitly pass a prop through every level of the tree. It is designed to share data that can be considered 'global' for a tree of React components, such as the current authenticated user, theme, or preferred language. Use it to avoid prop drilling for global data.

React / JSX
1// 1. Create Context
2const ThemeContext = React.createContext('light');
3
4// 2. Provide Context
5function App() {
6 return (
7 <ThemeContext.Provider value="dark">
8 <Toolbar />
9 </ThemeContext.Provider>
10 );
11}
12
13// 3. Consume Context
14function Toolbar() {
15 const theme = useContext(ThemeContext);
16 return <div className={theme}>Current theme: {theme}</div>;
17}
6

What is Redux and why use it?

Redux is a predictable state container for JavaScript apps. It helps write applications that behave consistently by storing the entire state of the application in a single central store. State updates are triggered by dispatching 'actions' which are handled by pure functions called 'reducers'. It's useful for complex applications with frequent state updates, deep component trees, or when different parts of the app need to access the same state.

React / JSX
1// Action
2const increment = () => ({ type: 'INCREMENT' });
3
4// Reducer
5const counter = (state = 0, action) => {
6 switch (action.type) {
7 case 'INCREMENT': return state + 1;
8 default: return state;
9 }
10};
11
12// Store
13const store = createStore(counter);
14
15// Component
16// dispatch(increment())
7

What are React keys, and why are they important?

Keys are special string attributes you need to include when creating lists of elements. They help React identify which items have changes, been added, or been removed. Keys give the elements a stable identity, which is crucial for efficient reconciliation/DOM updates. Using index as a key is discouraged if the order of items may change, as it can negatively impact performance and component state.

React / JSX
1const todoItems = todos.map((todo) => (
2 // Correct: Stable unique ID
3 <li key={todo.id}>
4 {todo.text}
5 </li>
6));
7
8// Avoid if list changes:
9// <li key={index}>{todo.text}</li>
8

Usage of TypeScript and ESLint in React

TypeScript adds static typing to React, allowing you to define shapes for props (Interfaces) and state. This catches errors at compile time and improves developer experience with autocompletion. ESLint is a linter that finds and fixes problems in your code. In React, specific plugins (like eslint-plugin-react-hooks) ensure you follow best practices, such as correctly declaring hook dependencies to prevent bugs.

React / JSX
1// TypeScript Interface
2interface ButtonProps {
3 label: string;
4 onClick: () => void;
5 disabled?: boolean; // Optional
6}
7
8const Button: React.FC<ButtonProps> = ({ label, onClick }) => (
9 <button onClick={onClick}>{label}</button>
10);
11
12// ESLint helps catch:
13// useEffect(() => { ... }, []) // Warning: missing dependency
9

How to optimize a React application?

Optimization techniques include: 1) Memoization using React.memo, useMemo, and useCallback to prevent unnecessary re-renders. 2) Code-splitting using React.lazy and Suspense to load components only when needed. 3) Virtualization (e.g., react-window) for rendering large lists efficiently. 4) Keeping state local when possible. 5) Optimizing images and assets. 6) Using the Production Build to minify code.

React / JSX
1// Code Splitting
2const OtherComponent = React.lazy(() => import('./OtherComponent'));
3
4function MyComponent() {
5 return (
6 <React.Suspense fallback={<div>Loading...</div>}>
7 <OtherComponent />
8 </React.Suspense>
9 );
10}
11
12// Memoization
13const MyComponent = React.memo(function MyComponent(props) {
14 /* render using props */
15});
10

What are Higher-Order Components (HOC)?

A Higher-Order Component (HOC) is a function that takes a component and returns a new component, reusing component logic. They were common class-based patterns for cross-cutting concerns (like logging or auth) but are often replaced by Hooks today.

React / JSX
1// HOC function
2function withLog(WrappedComponent) {
3 return function(props) {
4 console.log('Rendered');
5 return <WrappedComponent {...props} />;
6 }
7}
8
9// Usage
10const EnhancedButton = withLog(Button);
11

What is the Render Props pattern?

Render Props is a technique for sharing code between React components using a prop whose value is a function. A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render logic.

React / JSX
1<DataProvider render={(data) => (
2 <h1>Hello {data.target}</h1>
3)}/>
12

What are React Fragments and why use them?

Fragments allow you to group a list of children without adding extra nodes to the DOM. React components must return a single parent. Fragments (<>...</>) let you return multiple elements without wrapping them in a redundant <div>.

React / JSX
1return (
2 <>
3 <td>Hello</td>
4 <td>World</td>
5 </>
6);
13

What are Error Boundaries?

Error Boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI. They serve as a catch block for the component tree. Note: They must be class components.

React / JSX
1class ErrorBoundary extends React.Component {
2 componentDidCatch(error, info) {
3 logError(error);
4 }
5 render() {
6 if (this.state.hasError) return <h1>Error</h1>;
7 return this.props.children;
8 }
9}
14

What are React Portals?

Portals provide a way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. Useful for modals, tooltips, or popups to break out of overflow:hidden or z-index constraints.

React / JSX
1ReactDOM.createPortal(
2 <div className="modal">{children}</div>,
3 document.getElementById('modal-root')
4);
15

Difference between SSR, CSR, and SSG?

CSR (Client-Side Rendering) builds UI in the browser (good for Apps). SSR (Server-Side Rendering) builds HTML on server per request (good for SEO). SSG (Static Site Generation) builds HTML at build time (fastest, good for blogs). Next.js supports all three.

React / JSX
1// SSG
2export async function getStaticProps() { ... }
3
4// SSR
5export async function getServerSideProps() { ... }
16

Class Components vs Functional Components

Class components help manage state and lifecycle methods using ES6 classes. Functional components are plain JS functions using Hooks for state. Functional components are now the standard due to simpler syntax and hooks support.

React / JSX
1// Class
2class Welcome extends React.Component {
3 render() { return <h1>Hi</h1>; }
4}
5
6// Functional
7const Welcome = () => <h1>Hi</h1>;
17

What is a Custom Hook?

A Custom Hook is a JavaScript function starting with 'use' that calls other hooks. It extracts reusable logic (like fetching data or tracking window size) from components, keeping them clean and DRY.

React / JSX
1function useWindowSize() {
2 const [size, setSize] = useState(window.innerWidth);
3 // ... listener logic
4 return size;
5}
18

What are Synthetic Events?

Synthetic Events are React's cross-browser wrapper around the browser's native event. They ensure events work identically across all browsers and have the same interface as native events (e.g., preventDefault()).

React / JSX
1function handleClick(e) {
2 e.preventDefault(); // Works everywhere
3 console.log('Clicked');
4}
19

What is Strict Mode in React?

StrictMode highlights potential problems in an application. It runs checks in development mode only, such as detecting unsafe lifecycles or unexpected side effects by double-invoking constructors and hooks.

React / JSX
1<React.StrictMode>
2 <App />
3</React.StrictMode>
20

What is JSX?

JSX is a syntax extension for JavaScript that looks like HTML. It makes writing UI components intuitive. Under the hood, it compiles to React.createElement() calls.

React / JSX
1const element = <h1>Hello</h1>;
2// becomes
3React.createElement('h1', null, 'Hello');

Ready to Ace Your Interview?

Review more questions, practice coding problems, and get ready for your next big opportunity.

Explore More Topics

© 2026 GyaanSetu. Helping developers ace their interviews.