React Interview
Questions & Answers
🌱Beginner QuestionsQ1–Q10
React is an open-source JavaScript library (not a framework) for building user interfaces, created by Meta (Facebook) in 2013. It focuses on the View layer of an application.
- Component-based: UIs are built from small, reusable, isolated pieces called components.
- Declarative: You describe what the UI should look like, and React figures out how to update the DOM efficiently.
- Virtual DOM: React maintains a lightweight copy of the DOM in memory, diffing and batching updates for performance.
- Unidirectional data flow: Data flows down from parent to child via props, making state predictable and easy to debug.
- Large ecosystem: React Router, Redux, React Query, Next.js — massive tooling support.
JSX (JavaScript XML) is a syntax extension that lets you write HTML-like markup inside JavaScript. It's not HTML — it gets compiled by Babel into React.createElement() calls.
JSX rules to know: Use className not class. Use htmlFor not for. Every JSX expression must return a single root element (use <></> Fragment if needed). JavaScript expressions go inside {} curly braces.
The Virtual DOM is a lightweight JavaScript copy of the real DOM kept in memory. When state changes, React compares the new Virtual DOM against the previous version (a process called diffing), and only updates the parts of the real DOM that actually changed (reconciliation).
- Step 1: State or props change → React creates a new Virtual DOM tree.
- Step 2: React diffs the new tree against the previous one (using a reconciliation algorithm).
- Step 3: Only the changed nodes are updated in the real DOM — not the whole page.
Props (short for properties) are how you pass data from a parent component to a child component. They are read-only — a component must never modify its own props.
| Feature | State | Props |
|---|---|---|
| Owned by | The component itself | Parent component |
| Mutable | ✅ Yes (via setter) | ❌ No (read-only) |
| Triggers re-render | ✅ Yes when changed | ✅ Yes when changed by parent |
| Purpose | Internal component data | External configuration data |
Hooks (introduced in React 16.8) are functions that let you use state and other React features in functional components — without writing a class. They always start with use.
useEffect and when does it run?useEffect lets you perform side effects in functional components — data fetching, subscriptions, manually updating the DOM, or setting up timers. It runs after the browser has painted the screen.
| Feature | Controlled | Uncontrolled |
|---|---|---|
| Data stored in | React state | DOM (via ref) |
| Source of truth | React | DOM |
| Updated via | onChange + setState | ref.current.value |
| Validation | Easy — on every keystroke | On submit only |
| Best for | Most form use cases | File inputs, simple forms |
Keys are special string attributes that help React identify which items in a list have changed, been added, or removed. They allow React to efficiently reorder and update list items without re-rendering the entire list.
Every React component goes through three lifecycle phases. In modern React (functional components + hooks), useEffect handles all lifecycle events.
⚡Intermediate QuestionsQ11–Q21
Context provides a way to pass data through the component tree without passing props down manually at every level (prop drilling). It's ideal for global data like themes, authentication, locale, or user preferences.
useReducer and when would you use it over useState?useReducer is an alternative to useState for managing complex state logic. It uses a reducer function — a pure function that takes the current state and an action, and returns the next state.
Use useReducer when: state has multiple related sub-values, the next state depends on complex logic, or when you want to co-locate state transitions in one place (easier to test).
useMemo and useCallback?| Hook | Returns | Use case |
|---|---|---|
useMemo | A memoized value | Avoid re-computing expensive calculations |
useCallback | A memoized function | Stable function ref for child props / deps |
useMemo/useCallback when you have a measured performance problem. Premature memoisation adds complexity with no benefit.useRef and what are its main use cases?useRef returns a mutable object with a .current property. It persists across renders but does not trigger a re-render when changed.
Prop drilling happens when you pass props through multiple intermediate components that don't use the data themselves — just to get it to a deeply nested component.
React.memo is a higher-order component that wraps a functional component and skips re-rendering if its props haven't changed (shallow comparison). It's the functional equivalent of PureComponent.
Custom hooks are JavaScript functions that start with use and can call other hooks. They let you extract and reuse stateful logic across multiple components without changing the component hierarchy.
React Router is the standard routing library for React. Client-side routing updates the URL and renders different components without a full page reload, using the browser's History API.
| Feature | Class Component | Functional Component |
|---|---|---|
| Syntax | class X extends Component | function X(props) |
| State | this.state + setState() | useState() hook |
| Lifecycle | Lifecycle methods | useEffect() hook |
| Performance | Slightly more overhead | Lighter, modern default |
| Recommended | Legacy codebases | ✅ Modern React default |
React.lazy enables code splitting — loading components only when they're needed, reducing the initial bundle size. Suspense shows a fallback UI while the lazy component loads.
useEffect and useLayoutEffect?| Hook | When it fires | Use case |
|---|---|---|
useEffect | After browser has painted (asynchronous) | Data fetching, subscriptions, timers |
useLayoutEffect | After DOM mutation, before browser paints (synchronous) | DOM measurements, tooltip positioning |
useEffect. Use useLayoutEffect only when you need to read or modify the DOM before the user sees it, to prevent visual flickering.🔥Advanced QuestionsQ22–Q30
React Fiber is the reconciliation engine introduced in React 16. It completely rewrote how React processes component updates to enable incremental rendering — the ability to split rendering work into chunks and spread it over multiple frames.
- Problem with old reconciler (Stack): Rendering was synchronous and couldn't be interrupted. Large trees would block the main thread, causing janky UIs.
- Fiber solution: Work is broken into units (fibers). React can pause work, return to it later, prioritise certain updates, and abort work that's no longer needed.
- Priority levels: User interactions (clicks, typing) get higher priority than background data updates.
- Concurrent Mode (React 18): Built on Fiber — enables concurrent rendering,
startTransition,useTransition, and automatic batching.
useTransition possible.React 18 shipped with several major concurrent features that improve responsiveness:
A Higher-Order Component is a function that takes a component and returns a new enhanced component. It's a pattern for reusing component logic. Common in older React code — largely replaced by custom hooks in modern React.
React.memo, connect in Redux).Reconciliation is the process React uses to update the DOM efficiently by comparing the new Virtual DOM tree with the previous one (diffing). React applies two heuristics to make this O(n) instead of the naive O(n³) solution:
- Heuristic 1 — Element type: If two elements have different types (e.g.,
<div>vs<span>), React destroys the old subtree and builds a new one from scratch. - Heuristic 2 — Keys: When rendering lists, React uses
keyprops to match elements across renders. Same key = same element, reuse it. Different/missing key = destroy and rebuild.
Error Boundaries are class components that catch JavaScript errors anywhere in their child component tree, log them, and display a fallback UI instead of crashing the whole application.
React performance optimisation covers multiple layers:
- CSR (Client-Side Rendering): Browser downloads empty HTML + JS bundle, React renders everything in the browser. Slow initial load, bad SEO.
- SSR (Server-Side Rendering): Server renders HTML on each request, sends fully-formed HTML to browser. Fast first paint, good SEO. React then "hydrates" it (attaches event handlers).
- SSG (Static Site Generation): HTML pre-rendered at build time. Ultra-fast, perfect for content that rarely changes.
| Feature | Server Component | Client Component |
|---|---|---|
| Runs on | Server only | Browser (+ server for hydration) |
| Bundle size | Zero JS sent to client | Included in JS bundle |
| Can use hooks? | ❌ No | ✅ Yes |
| Can use state/effects? | ❌ No | ✅ Yes |
| Direct DB/FS access? | ✅ Yes | ❌ No |
| Directive | Default in Next.js App Router | 'use client' at top |
Up Next: HTML & CSS Interview Questions
You've covered React. The next page in the series covers HTML semantics, CSS Flexbox, Grid, animations, specificity, and responsive design. Coming soon.
← Python Q&A