Basics of React
Basics of React Interview with follow-up questions
1. What is React and why is it used?
React is an open-source JavaScript library, maintained by Meta and a large community, for building user interfaces out of reusable components. You describe what the UI should look like for a given state, and React keeps the DOM in sync as that state changes.
Key reasons it's used:
- Component model – UI is composed from small, reusable, testable pieces.
- Declarative – you describe the what, not the step-by-step DOM mutations; React reconciles the difference for you.
- Efficient updates – a virtual DOM + reconciliation (and, as of React 19, the React Compiler) minimise actual DOM work.
- Huge ecosystem – routing, data fetching, state management, and full-stack frameworks (Next.js, React Router v7) build on top of it.
- Cross-platform – the same mental model powers web (react-dom) and native mobile (React Native).
Modern React (19.x) is function-components-and-hooks first; class components still work but are legacy.
Follow-up 1
What is the virtual DOM in React?
The virtual DOM is a lightweight copy of the actual DOM (Document Object Model) in memory. React uses the virtual DOM to efficiently update and render components. When there is a change in the application state, React compares the virtual DOM with the previous version to determine the minimal set of changes needed to update the actual DOM. This approach improves performance by reducing the number of direct manipulations of the actual DOM.
Follow-up 2
Can you explain the component-based architecture in React?
In React, the UI is divided into reusable components. A component is a self-contained module that encapsulates its own logic and UI. Components can be composed together to build complex UIs. The component-based architecture in React promotes reusability, modularity, and maintainability. Each component can have its own state and lifecycle methods, allowing for easy management of data and interactions.
Follow-up 3
What are the key features of React?
Some of the key features of React are:
- Virtual DOM: React uses a virtual DOM to efficiently update and render components.
- Component-based architecture: React promotes the use of reusable and modular components.
- Declarative syntax: React uses a declarative syntax, making it easier to understand and reason about the UI.
- One-way data flow: React follows a one-way data flow, making it easier to track and manage data changes.
- Unidirectional data flow: React follows a unidirectional data flow, making it easier to track and manage data changes.
- React Native: React can be used to build native mobile applications using React Native.
- Large ecosystem: React has a large and active community, with a wide range of libraries and tools available for development.
2. What is JSX in React?
JSX is a syntax extension for JavaScript that lets you write HTML-like markup directly inside your JavaScript/TypeScript code. It's not understood by the browser — a compiler (Babel, SWC, or TypeScript) transforms it into plain function calls.
const greeting = <h1>Hello, {name}!</h1>;
// compiles to roughly:
// const greeting = jsx("h1", { className: "title", children: ["Hello, ", name, "!"] });
Key points:
- Expressions go inside
{ }; attributes use camelCase (className,onClick). - A component must return a single root — use a Fragment (
<>...>) to avoid an extra wrapper node. - JSX is just syntactic sugar; you can write React without it, but it's the standard because it keeps markup and logic readable and co-located.
Since React 17 the compiler uses the automatic JSX runtime (react/jsx-runtime), so you no longer need to import React just to use JSX.
Follow-up 1
What are the advantages of using JSX?
There are several advantages of using JSX in React:
- Easy to understand: JSX combines the power of JavaScript and HTML, making it easier to understand and write code.
- Component-based structure: JSX allows you to create reusable UI components, which makes it easier to maintain and update your code.
- Performance optimization: JSX is compiled to optimized JavaScript code, which helps in improving the performance of React applications.
- Static type checking: JSX supports static type checking with tools like TypeScript and Flow, which helps in catching errors during development.
- Rich ecosystem: JSX is widely adopted in the React community, which means there are plenty of resources, libraries, and tools available to support JSX development.
Follow-up 2
Can you write React without JSX?
Yes, it is possible to write React without JSX. JSX is just a syntactic sugar for writing React components. You can write React components using plain JavaScript by using the React.createElement() function. However, JSX is recommended as it provides a more concise and readable way to define components.
Follow-up 3
How does JSX differ from HTML?
JSX and HTML are similar in syntax, but there are a few key differences:
- Attribute names: In JSX, attribute names are written in camelCase, while in HTML, they are written in lowercase. For example,
classin HTML becomesclassNamein JSX. - Style and event handling: In JSX, inline styles are defined using JavaScript objects, and event handlers are written in camelCase. In HTML, styles are defined using CSS syntax, and event handlers are written in lowercase.
- Comments: JSX supports JavaScript-style comments (
{/* comment */}), while HTML uses HTML-style comments (``). - Self-closing tags: In JSX, self-closing tags must be explicitly closed with a slash (
<br>), while in HTML, self-closing tags can be written without a closing slash (<br>).
3. What are components in React?
Components are the building blocks of a React app — independent, reusable pieces that return a piece of UI. There are two kinds:
- Function components (the modern default): plain functions that take
propsand return JSX. State and lifecycle come from Hooks (useState,useEffect, etc.).
function Welcome({ name }) {
return <h1>Hello, {name}</h1>;
}
- Class components (legacy): extend
React.Componentand define arender()method. Still supported, but new code should prefer function components. The one thing only classes can do is be an error boundary (viacomponentDidCatch/getDerivedStateFromError).
Components compose into trees, accept data via props, and can hold their own state. With React Server Components (stable in 19), a component can also run only on the server and send rendered output to the client.
Follow-up 1
How do you pass data to components?
Data can be passed to components in React through props. Props are read-only and are used to pass data from a parent component to its child components. The parent component can pass any data or functions as props, and the child component can access and use them. Here's an example:
// Parent component
function App() {
const name = 'John';
return (
);
}
// Child component
function ChildComponent(props) {
return (
<h1>Hello, {props.name}!</h1>
);
}
Follow-up 2
What is the difference between functional and class components?
Functional components are simple JavaScript functions that return JSX. They are easier to write and understand, and they don't have their own state or lifecycle methods. Class components, on the other hand, are ES6 classes that extend the React.Component class. They have their own state and lifecycle methods, and are used when you need more advanced features like state management or lifecycle hooks.
Follow-up 3
What is the lifecycle of a component in React?
The lifecycle of a component in React refers to the different stages a component goes through from its creation to its removal from the DOM. There are three main phases in the component lifecycle: Mounting, Updating, and Unmounting.
Mounting: This phase occurs when a component is being created and inserted into the DOM. The lifecycle methods in this phase are:
- constructor()
- static getDerivedStateFromProps()
- render()
- componentDidMount()
Updating: This phase occurs when a component is being re-rendered due to changes in props or state. The lifecycle methods in this phase are:
- static getDerivedStateFromProps()
- shouldComponentUpdate()
- render()
- getSnapshotBeforeUpdate()
- componentDidUpdate()
Unmounting: This phase occurs when a component is being removed from the DOM. The lifecycle method in this phase is:
- componentWillUnmount()
These lifecycle methods can be used to perform certain actions at specific points in a component's lifecycle, such as fetching data, updating the UI, or cleaning up resources.
4. How does React handle events?
React uses a synthetic event system: a cross-browser wrapper around the native DOM event with a consistent API. You attach handlers declaratively with camelCase props:
function Button() {
const handleClick = (e) => console.log("clicked", e.type);
return Click me;
}
How it works:
- React attaches one listener per event type at the root container (since React 17 — not at
document) and uses event delegation to route events to the right component. - The handler receives a
SyntheticEvent; calle.preventDefault()ore.stopPropagation()as usual. - Note: the old "event pooling" optimisation was removed in React 17, so you can safely read the event asynchronously — no need to call
e.persist().
This gives you native-feeling events with consistent behaviour across browsers.
Follow-up 1
How does event handling in React differ from traditional DOM?
Event handling in React differs from traditional DOM in a few ways:
- React uses synthetic events instead of native browser events. Synthetic events are a cross-browser compatible abstraction layer that provides consistent event handling across different browsers.
- React uses a single event listener at the root of the document, which improves performance compared to attaching event listeners to individual elements.
- React uses event delegation to handle events. Instead of attaching event listeners to every element, React captures events at the root and delegates them to the appropriate component.
Follow-up 2
What is event pooling in React?
Event pooling is a technique used by React to improve performance when handling events. Instead of creating a new synthetic event object for each event, React reuses the same event object and updates its properties with new values. This reduces the memory footprint and improves the overall performance of event handling in React.
Follow-up 3
Can you give an example of handling an onClick event in React?
Sure! Here's an example of handling an onClick event in React:
import React from 'react';
class MyComponent extends React.Component {
handleClick() {
console.log('Button clicked!');
}
render() {
return (
Click me
);
}
}
5. What is the significance of keys in React?
Keys are special string/number props you give to each item when rendering a list. They give each element a stable identity across renders so React's reconciler can tell which items were added, removed, or reordered — and reuse/update DOM and component state correctly instead of rebuilding the list.
{users.map((u) => <li>{u.name}</li>)}
Best practices:
- Use a stable, unique id from your data (e.g.
u.id). - Avoid the array index as a key when the list can reorder, insert, or delete — it causes wrong state/DOM reuse and subtle bugs.
- Keys only need to be unique among siblings, not globally.
Good keys prevent unnecessary DOM operations and preserve component state (form inputs, focus) when lists change.
Follow-up 1
What happens if you don't provide a key in a list?
If you don't provide a key in a list, React will display a warning in the console. React uses keys to track the identity of each element in the list, so omitting keys can lead to unexpected behavior and performance issues. It is recommended to always provide a unique key for each element in a list.
Follow-up 2
Can you use index as a key?
While it is possible to use the index of an element in a list as a key, it is generally not recommended. Using the index as a key can cause issues when the list order changes or when new items are added or removed. It is best to use a unique identifier for each element as the key, such as an ID from the data source.
Follow-up 3
What is the best way to assign keys in a list?
The best way to assign keys in a list is to use a unique identifier from the data source. If the data items in the list have a unique ID property, it is recommended to use that as the key. If the data items do not have a unique ID, you can generate a unique key using a library like uuid or by combining multiple properties to create a composite key.
Live mock interview
Mock interview: Basics of React
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.