Top React.js Interview Questions (Updated for 2025)
Updated for 2025! Curated top React.js interview questions with high quality answers for acing your Front End Engineer interviews, brought to you by GreatFrontEnd.
Black Friday 2025 sale going on now, enjoy the largest discount of the year! Get 30% off GreatFrontEnd Premium -
Table of Contents
| No. | Questions |
|---|---|
| 1 | What is React? Describe the benefits of React |
| 2 | What is the difference between React Node, React Element, and a React Component? |
| 3 | What is JSX and how does it work? |
| 4 | What is the difference between state and props in React? |
| 5 | What is the purpose of the key prop in React? |
| 6 | What is the consequence of using array indices as the value for keys in React? |
| 7 | What is the difference between controlled and uncontrolled React Components? |
| 8 | What are some pitfalls about using context in React? |
| 9 | What are the benefits of using hooks in React? |
| 10 | What are the rules of React hooks? |
| 11 | What is the difference between useEffect and useLayoutEffect in React? |
| 12 | What is the purpose of callback function argument format of setState() in React and when should it be used? |
| 13 | What does the dependency array of useEffect affect? |
| 14 | What is the useRef hook in React and when should it be used? |
| 15 | What is the useCallback hook in React and when should it be used? |
| 16 | What is the useMemo hook in React and when should it be used? |
| 17 | What is the useReducer hook in React and when should it be used? |
| 18 | What is the useId hook in React and when should it be used? |
| 19 | What does re-rendering mean in React? |
| 20 | What are React Fragments used for? |
| 21 | What is forwardRef() in React used for? |
| 22 | How do you reset a component's state in React? |
| 23 | Why does React recommend against mutating state? |
| 24 | What are error boundaries in React for? |
| 25 | How do you test React applications? |
| 26 | Explain what React hydration is |
| 27 | What are React Portals used for? |
| 28 | How do you debug React applications? |
| 29 | What is React strict mode and what are its benefits? |
| 30 | How do you localize React applications? |
| 31 | What is code splitting in a React application? |
| 32 | How would one optimize the performance of React contexts to reduce rerenders? |
| 33 | What are higher order components in React? |
| 34 | What is the Flux pattern and what are its benefits? |
| 35 | Explain one-way data flow of React and its benefits |
| 36 | How do you handle asynchronous data loading in React applications? |
| 37 | Explain server-side rendering of React applications and its benefits? |
| 38 | Explain static generation of React applications and its benefits? |
| 39 | Explain the presentational vs container component pattern in React |
| 40 | What are some common pitfalls when doing data fetching in React? |
| 41 | What are render props in React and what are they for? |
| 42 | What are some React anti-patterns? |
| 43 | How do you decide between using React state, context, and external state managers? |
| 44 | Explain the composition pattern in React |
| 45 | What is virtual DOM in React? |
| 46 | How does virtual DOM in React work? What are its benefits and downsides? |
| 47 | What is React Fiber and how is it an improvement over the previous approach? |
| 48 | What is reconciliation in React? |
| 49 | What is React Suspense and what does it enable? |
| 50 | Explain what happens when the useState setter function is called in React |
Questions with answers
-
What is React? Describe the benefits of React
React is a JavaScript library created by Facebook for building user interfaces, primarily for single-page applications. It allows developers to create reusable components that manage their own state. Key benefits of React include a component-based architecture for modular code, the virtual DOM for efficient updates, a declarative UI for more readable code, one-way data binding for predictable data flow, and a strong community and ecosystem with abundant resources and tools.
Key characteristics of React:
- Declarative: You describe the desired state of your UI based on data, and React handles updating the actual DOM efficiently.
- Component-based: Build reusable and modular UI elements (components) that manage their own state and logic.
- Virtual DOM: React uses a lightweight in-memory representation of the actual DOM, allowing it to perform updates selectively and efficiently.
- JSX: While not mandatory, JSX provides a syntax extension that allows you to write HTML-like structures within your JavaScript code, making UI development more intuitive.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the difference between React Node, React Element, and a React Component?
A React Node is any renderable unit in React, such as an element, string, number, or
null. A React Element is an immutable object describing what to render, created using JSX orReact.createElement. A React Component is a function or class that returns React Elements, enabling the creation of reusable UI pieces.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is JSX and how does it work?
JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows you to write HTML-like code within JavaScript. JSX makes it easier to create React components by allowing you to write what looks like HTML directly in your JavaScript code. Under the hood, JSX is transformed into JavaScript function calls, typically using a tool like Babel. For example,
in JSX is transformed intoHello, world!React.createElement('div', null, 'Hello, world!').
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the difference between state and props in React?
In React,
stateis a local data storage that is managed within a component and can change over time, whilepropsare read-only attributes passed from a parent component to a child component. State is used for data that changes within a component, whereas props are used to pass data and event handlers to child components.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the purpose of the
keyprop in React?The
keyprop in React is used to uniquely identify elements in a list. It helps React optimize rendering by efficiently updating and reordering elements. Without a uniquekey, React may re-render elements unnecessarily, leading to performance issues and bugs.{
items.map((item) => <ListItem key={item.id} value={item.value} />);
}
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the consequence of using array indices as the value for
keys in React?Using array indices as the value for
keys in React can lead to performance issues and bugs. When the order of items changes, React may not correctly identify which items have changed, leading to unnecessary re-renders or incorrect component updates. It's better to use unique identifiers forkeys to ensure React can efficiently manage and update the DOM.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the difference between controlled and uncontrolled React Components?
Controlled components in React are those where the form data is handled by the component's state. The state is the single source of truth, and any changes to the form input are managed through event handlers. Uncontrolled components, on the other hand, store their own state internally and rely on refs to access the form values. Controlled components offer more control and are easier to test, while uncontrolled components can be simpler to implement for basic use cases.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What are some pitfalls about using context in React?
Using context in React can lead to performance issues if not managed properly. It can cause unnecessary re-renders of components that consume the context, even if the part of the context they use hasn't changed. Additionally, overusing context for state management can make the code harder to understand and maintain. It's important to use context sparingly and consider other state management solutions like Redux or Zustand for more complex state needs.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What are the benefits of using hooks in React?
Hooks in React allow you to use state and other React features without writing a class. They make it easier to reuse stateful logic between components, improve code readability, and simplify the codebase by reducing the need for lifecycle methods. Hooks like
useStateanduseEffectare commonly used to manage state and side effects in functional components.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What are the rules of React hooks?
React hooks have a few essential rules to ensure they work correctly. Always call hooks at the top level of your React function, never inside loops, conditions, or nested functions. Only call hooks from React function components or custom hooks. These rules ensure that hooks maintain the correct state and lifecycle behavior.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the difference between
useEffectanduseLayoutEffectin React?useEffectanduseLayoutEffectare React hooks used to handle side effects in functional components, but they differ in timing and use cases:useEffect: Runs asynchronously after the DOM has been painted. It is suitable for tasks like data fetching, subscriptions, or logging.useLayoutEffect: Runs synchronously after DOM mutations but before the browser paints. Use it for tasks like measuring DOM elements or synchronizing the UI with the DOM.
Code example:
{ console.log('useEffect: Runs after DOM paint'); }); useLayoutEffect(() => { console.log('useLayoutEffect: Runs before DOM paint'); console.log('Element width:', ref.current.offsetWidth); }); returnHello; }">import React, { useEffect, useLayoutEffect, useRef } from 'react';
function Example() {
const ref = useRef();
useEffect(() => {
console.log('useEffect: Runs after DOM paint');
});
useLayoutEffect(() => {
console.log('useLayoutEffect: Runs before DOM paint');
console.log('Element width:', ref.current.offsetWidth);
});
return <div ref={ref}>Hellodiv>;
}
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the purpose of callback function argument format of
setState()in React and when should it be used?The callback function argument format of
setState()in React is used to ensure that state updates are based on the most recent state and props. This is particularly important when the new state depends on the previous state. Instead of passing an object directly tosetState(), you pass a function that takes the previous state and props as arguments and returns the new state.this.setState((prevState, props) => ({
counter: prevState.counter + props.increment,
}));
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What does the dependency array of
useEffectaffect?The dependency array of
useEffectdetermines when the effect should re-run. If the array is empty, the effect runs only once after the initial render. If it contains variables, the effect runs whenever any of those variables change. If omitted, the effect runs after every render.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the
useRefhook in React and when should it be used?The
useRefhook in React is used to create a mutable object that persists across renders. It can be used to access and manipulate DOM elements directly, store mutable values that do not cause re-renders when updated, and keep a reference to a value without triggering a re-render. For example, you can useuseRefto focus an input element:{ inputEl.current.focus(); }, []); return ; }">import React, { useRef, useEffect } from 'react';
function TextInputWithFocusButton() {
const inputEl = useRef(null);
useEffect(() => {
inputEl.current.focus();
}, []);
return <input ref={inputEl} type="text" />;
}
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the
useCallbackhook in React and when should it be used?The
useCallbackhook in React is used to memoize functions, preventing them from being recreated on every render. This is particularly useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders. You should useuseCallbackwhen you have a function that is passed as a prop to a child component and you want to avoid the child component re-rendering unnecessarily.const memoizedCallback = useCallback(() => {
doSomething(a, b);
}, [a, b]);
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the
useMemohook in React and when should it be used?The
useMemohook in React is used to memoize expensive calculations so that they are only recomputed when one of the dependencies has changed. This can improve performance by avoiding unnecessary recalculations. You should useuseMemowhen you have a computationally expensive function that doesn't need to run on every render.const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the
useReducerhook in React and when should it be used?The
useReducerhook in React is used for managing complex state logic in functional components. It is an alternative touseStateand is particularly useful when the state has multiple sub-values or when the next state depends on the previous one. It takes a reducer function and an initial state as arguments and returns the current state and a dispatch function.const [state, dispatch] = useReducer(reducer, initialState);Use
useReducerwhen you have complex state logic that involves multiple sub-values or when the next state depends on the previous state.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
Back to top |
-
What is the
useIdhook in React and when should it be used?The
useIdhook in React is used to generate unique IDs for elements within a component. This is particularly useful for accessibility purposes, such as linking form inputs with their labels. It ensures that IDs are unique across the entire application, even if the component is rendered multiple times.); }">import { useId } from 'react';
function MyComponent() {
const id = useId();
return (
<div>
<label htmlFor={id}>Name:label>
<input id={id} type="text" />
div>
);
}