Top React Interview Questions

ReactFrameworkFrontend

React is an open-source JavaScript library developed by Facebook for building user interfaces, especially for developing single-page applications. It allows developers to create large web applications that can update and render efficiently in response to data changes.

ReactComponentsUI

Components are the building blocks of a React application. Each component is an independent and reusable piece of UI. There are two types of components:

  • Functional Components – simple functions that return JSX.

  • Class Components – ES6 classes that extend React.Component

ReactJSXSyntax

JSX stands for JavaScript XML. It’s a syntax extension that looks similar to HTML and is used with React to describe what the UI should look like. JSX allows mixing HTML with JavaScript.

 

Example:

const element = <h1>Hello, JSX!</h1>;

ReactVirtual DOMRendering

The virtual DOM is a lightweight in-memory representation of the real DOM. When a component's state or props change, React first updates the virtual DOM. Then, it calculates the difference (diffing) and updates only the changed parts in the real DOM, improving performance.

ReactPropsComponent Communication

Props (short for properties) are used to pass data from parent to child components. They are read-only and help in making components reusable.

In simple words, we can say React Props are like function arguments in JavaScript and attributes in HTML.

To send props into a component, we use the same syntax as HTML attributes:

Example:

Add a "language" props to the Codegrill  Component:

<Codegrill language="React" />;

The component receives the argument as a props object:

Example:

function Codegrill (props) {
 return <h2>Welcome to { props.language } world!</h2>;
}

ReactStateComponent State

State is a built-in object that stores property values that belong to the component. When the state changes, the component re-renders.

Example:

 

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}

ReactStatePropsComponent Communication
  • Props are used to pass data to a component from its parent.
  • State is managed within the component and can change over time.

 

Feature Props State
Changeable No Yes
Set by Parent Component
Usage Reusability Interactivity

ReactHooksFunctional Components

Hooks are functions that let you “hook into” React features in functional components. Common hooks include useState, useEffect, useContext, etc.

Example:

useEffect(() => {
 document.title = "Hello!";
}, );

ReactFunctional ComponentsClass Components
  • Functional Components are simple functions that return JSX.
  • Class Components are ES6 classes that extend React.Component and can have lifecycle methods and state (prior to hooks).

With hooks, functional components can manage state and side effects too.

ReactuseStateHooksState Management

useState is a hook that allows you to add state to a functional component. It returns an array with the current state and a function to update it.

Example:

const [count, setCount] = useState(0);

ReactuseEffectHooksSide Effects

useEffect is used for side effects like fetching data, subscribing to services, or manually changing the DOM. It runs after every render by default.

Example:

useEffect(() => {
 console.log("Component mounted or updated");
}, );

ReactConditional RenderingUI

We can use if, ternary (? :), or logical (&&) operators inside JSX to conditionally render components.

Example:

{isLoggedIn ? <Logout /> : <Login />}

ReactKey PropList Rendering

Keys help React identify which items have changed, are added, or removed in a list. It helps in efficiently updating the UI.

Example:

{items.map(item => <li key={item.id}>{item.name}</li>)}

ReactFragmentsJSX

Fragments let you group multiple elements without adding an extra node to the DOM.

Example:

<>
 <h1>Title</h1>
 <p>Description</p>
</>

ReactLifting State UpState Management

When two components need to share state, it's moved to their closest common ancestor. This is called lifting state up.

ReactContextState Management

Context is a way to pass data through the component tree without having to pass props manually at every level.

Example:

const ThemeContext = React.createContext('light');

ReactProp DrillingState Management

Prop drilling is passing props through many layers of components unnecessarily. You can avoid it using context or state management libraries like Redux.

ReactReduxState Management

Redux is a state management library often used with React. It stores the entire application state in a single store, and state is updated using actions and reducers.

ReactFormsUser Input

Forms in React are controlled using state. Each input field's value is managed by state and updated via an onChange handler.

ReactControlled ComponentsUncontrolled ComponentsForms
  • Controlled components: React controls the form data using state.
  • Uncontrolled components: The DOM manages the input state using ref.

ReactEvent HandlingUser Interaction

React uses camelCase for event names and passes functions directly

Example:

<button onClick={handleClick}>Click Me</button>

ReactReconciliationRendering

Reconciliation is the process React uses to update the DOM. It compares the new virtual DOM with the previous one and makes the minimal changes.

ReactReconciliation AlgorithmPerformance

React’s reconciliation algorithm uses a diffing strategy on the virtual DOM to efficiently update only the changed parts of the actual DOM. Instead of re-rendering the entire DOM, it does the following things

Compares old and new virtual DOM trees.

Identifies differences (node type, attributes, keys).

Applies minimal updates using document.createElement, appendChild, etc.

This makes updates faster and less expensive than direct DOM manipulation.

ReactHigher-Order ComponentsComponent Composition

A Higher-Order Component is a function that takes a component and returns a new component with extended behavior.

const withLogger = (WrappedComponent) => {
 return (props) => {
   console.log('Props:', props);
   return <WrappedComponent {...props} />;
 };
};
const MyComponent = (props) => <div>Hello {props.name}</div>;
export default withLogger(MyComponent);

Used for cross-cutting concerns like authentication, logging, and analytics.

ReactContext APIReduxState Management

The Context API allows global state management without prop drilling. Ideal for simple use cases like theme, auth, or user settings.

 

const ThemeContext = createContext('light');
function App() {
 return (
   <ThemeContext.Provider value="dark">
     <Toolbar />
   </ThemeContext.Provider>
 );
}
function Toolbar() {
 const theme = useContext(ThemeContext);
 return <div>Theme is {theme}</div>;
}

Use Redux for complex, deeply nested, and large-scale applications.

ReactCode SplittingPerformance
Code splitting means loading components only when needed, reducing initial bundle size. jsx CopyEdit import React, { lazy, Suspense } from 'react'; const LazyComponent = lazy(() => import('./HeavyComponent')); function App() { return ( Loading...
}> ); } It improves performance and user experience, especially in large apps.

ReactCustom HooksHooks

Custom hooks are functions that start with use and encapsulate reusable logic.

 

function useCounter(initial = 0) {
 const [count, setCount] = useState(initial);
 const increment = () => setCount(c => c + 1);
 return { count, increment };
}

 Usage:

const { count, increment } = useCounter(5);

Custom hooks improve modularity and readability.

ReactReact.memoPerformance

React.memo is a HOC that prevents re-renders if props haven't changed.

 

const MyComponent = React.memo(({ name }) => {
 console.log('Codegrill');
 return <div>Hello {name}</div>;
});

Use it for functional components with pure props to optimize performance.

ReactLifting State UpState ManagementScalability

While lifting state up helps share state, in complex apps it can:
Lead to tight coupling

Make parent components bloated

Reduce reusability of child components

In such cases, Context API, Redux, or Recoil is preferred for better separation of concerns.

ReactRendering PerformanceOptimization
  • Virtual DOM minimizes real DOM operations.
  • Reconciliation efficiently updates the UI
  • Keys help track elements in lists.
  • Memoization via React.memo, useMemo, useCallback.
  • Code splitting with React.lazy and Suspense.

These mechanisms ensure smooth user experience even in dynamic UIs.

ReactRender PropsComponent Composition

A render prop is a function passed to a component that controls what to render.

function Mouse({ render }) {
 const [position, setPosition] = useState({ x: 0, y: 0 });
 return <div onMouseMove={e => setPosition({ x: e.clientX, y: e.clientY })}>
   {render(position)}
 </div>;
}
<Mouse render={({ x, y }) => <h1>Mouse is at {x}, {y}</h1>} />

This promotes reuse without HOCs or duplication.

ReactPortalsDOM Manipulation

React Portals render components outside the root DOM hierarchy.

ReactDOM.createPortal(
 <ModalContent />,
 document.getElementById('modal-root')
);

Usage:

  • Modals
  • Tooltips
  • Dropdowns

It allows overlay-like behavior without CSS hacks.

ReactuseMemouseCallbackHooksPerformance

useMemo memoizes the result of a computation.


useCallback memoizes the function definition.

 

Use useMemo when you have an expensive calculation:

 

const expensiveValue = useMemo(() => computeExpensiveValue(num), num);

Use useCallback when passing stable functions to child components:

 

const handleClick = useCallback(() => doSomething(id), id);

Both are performance optimizations and should be used when rerenders hurt performance.

ReactStrictModeDebugging

<React.StrictMode> is a development tool that helps:

  • Highlight potential problems
  • Detect unsafe lifecycle methods
  • Warn about deprecated patterns

    Verify side effects in useEffect by invoking them twice
    <React.StrictMode>
     <App />
    </React.StrictMode>

It does not affect production builds.

ReactProp DrillingState Management

Prop drilling happens when you pass data through many intermediate components that don’t need it, just to get it to the ones that do.

 

<Parent>
 <Child>
   <GrandChild data={data} />
 </Child>
</Parent>

This can be avoided by using

  • Context API
  • State management libraries (Redux, Zustand)
  • Custom hooks

ReactKeysList Rendering

Keys help React:

  • Track element identity
  • Optimize reconciliation
  • Avoid unnecessary re-renders
  • Use unique and stable keys like IDs:

     

    {users.map(user => (
     <li key={user.id}>{user.name}</li>
    ))}

Avoid using index as key unless items won’t change position.

ReactControlled ComponentsUncontrolled ComponentsForms

Controlled Component: Form state is handled by React.

const [value, setValue] = useState('');
<input value={value} onChange={e => setValue(e.target.value)} />

Uncontrolled Component: Form state is handled by the DOM.

<input ref={inputRef} />

Controlled components give better control and validation.

ReactDebouncingPerformance

Debouncing limits the rate a function is invoked. Useful for search or resize events.

function useDebounce(value, delay) {
 const [debounced, setDebounced] = useState(value);
 useEffect(() => {
   const handler = setTimeout(() => setDebounced(value), delay);
   return () => clearTimeout(handler);
 }, value);
 return debounced;
}

This can be used in search components to avoid API spamming.

ReactFragmentsJSX

Fragments let you return multiple elements without adding extra nodes.

<>
 <h1>I love Codegrill</h1>
 <p>Codegrill is awesome!</p>
</>

Or with a key:

<React.Fragment key={item.id}>
 <div>{item.name}</div>
</React.Fragment>

Useful when wrapping elements in lists or layouts.

ReactHydrationServer-Side Rendering

Hydration is the process of React attaching event listeners and making a static HTML page interactive.

  • SSR generates HTML on the server.
  • Hydration adds React behavior on the client.
  • Server generates HTML
  • React attaches events on client
  • If hydration fails (e.g., mismatch in DOM), React logs a warning.

 

ReactError BoundariesError Handling

Error boundaries catch errors in child components and render fallback UI.

class ErrorBoundary extends React.Component {
 state = { hasError: false };
 static getDerivedStateFromError(error) {
   return { hasError: true };
 }
 render() {
   return this.state.hasError
     ? <h1>Something went wrong.</h1>
     : this.props.children;
 }
}

This can be used to catch runtime errors in:

  • Component tree
  • Lazy components
  • External libraries