React Fundamentals Interview Questions
Table of Contents + −
In this article, we will cover the core React interview questions that build the base for almost every React discussion. These questions focus on JSX, Virtual DOM, React internals at a high level, props vs state basics, and the concepts interviewers usually ask first.
This page includes 24 questions in this topic group.
What is React?
React (aka React.js or ReactJS) is an open-source front-end JavaScript library for building user interfaces based on components. It’s used for handling the view layer in web and mobile applications, and allows developers to create reusable UI components and manage the state of those components efficiently.
React was created by Jordan Walke, a software engineer at Facebook (now Meta). It was first deployed on Facebook’s News Feed in 2011 and on Instagram in 2012. The library was open-sourced in May 2013 and has since become one of the most popular JavaScript libraries for building modern user interfaces.
-
What is the history behind React evolution?
The history of ReactJS started in 2010 with the creation of XHP. XHP is a PHP extension which improved the syntax of the language such that XML document fragments become valid PHP expressions and the primary purpose was used to create custom and reusable HTML elements.
The main principle of this extension was to make front-end code easier to understand and to help avoid cross-site scripting attacks. The project was successful to prevent the malicious content submitted by the scrubbing user.
But there was a different problem with XHP in which dynamic web applications require many roundtrips to the server, and XHP did not solve this problem. Also, the whole UI was re-rendered for small change in the application. Later, the initial prototype of React is created with the name FaxJ by Jordan inspired from XHP. Finally after sometime React has been introduced as a new library into JavaScript world.
See deep-dive answer
The evolution of React has a fascinating history that spans over a decade:2010-2011: The Origins
- The journey began with XHP, a PHP extension created at Facebook that allowed HTML components to be used in PHP code
- XHP improved front-end code readability and helped prevent cross-site scripting (XSS) attacks
- However, XHP had limitations with dynamic web applications, requiring frequent server roundtrips and complete UI re-renders for small changes
2011-2012: Early Development
- Jordan Walke created the first prototype called FaxJS (later renamed to React), inspired by XHP’s component model
- The key innovation was bringing XHP’s component model to JavaScript with performance improvements
- React introduced the Virtual DOM concept to solve the performance issues of full page re-renders
- First deployed internally on Facebook’s News Feed in 2011 and Instagram in 2012
2013: Public Release
- React was officially open-sourced at JSConf US in May 2013
- Initial public reception was mixed, with some developers skeptical about the JSX syntax and the approach of mixing markup with JavaScript
2014-2015: Growing Adoption
- React Native was announced in 2015, extending React’s paradigm to mobile app development
- The ecosystem began to grow with tools like Redux for state management
- Companies beyond Facebook began adopting React for production applications
2016-2018: Maturation
- React 16 (“Fiber”) was released in 2017 with a complete rewrite of the core architecture
- Introduction of new features like Error Boundaries, Portals, and improved server-side rendering
- React 16.3 introduced the Context API for easier state management
2019-Present: Modern React
- React Hooks were introduced in React 16.8 (February 2019), revolutionizing state management in functional components
- React 17 (October 2020) focused on making React upgrades easier
- React 18 (March 2022) introduced concurrent rendering and automatic batching
- React continues to evolve with Server Components, the new React compiler (React Forget), and other performance improvements
Note: JSX, React’s syntax extension, was indeed inspired by XHP’s approach of embedding XML-like syntax in code.
What are the major features of React?
React offers a powerful set of features that have made it one of the most popular JavaScript libraries for building user interfaces:
Core Features:
-
Component-Based Architecture: React applications are built using components - independent, reusable pieces of code that return HTML via a render function. This modular approach enables better code organization, reusability, and maintenance.
-
Virtual DOM: React creates an in-memory data structure cache, computes the resulting differences, and efficiently updates only the changed parts in the browser DOM. This approach significantly improves performance compared to direct DOM manipulation.
-
JSX (JavaScript XML): A syntax extension that allows writing HTML-like code in JavaScript. JSX makes the code more readable and expressive while providing the full power of JavaScript.
-
Unidirectional Data Flow: React follows a one-way data binding model where data flows from parent to child components. This makes the code more predictable and easier to debug.
-
Declarative UI: React allows you to describe what your UI should look like for a given state, and it handles the DOM updates when the underlying data changes.
Advanced Features:
-
React Hooks: Introduced in React 16.8, hooks allow using state and other React features in functional components without writing classes.
-
Context API: Provides a way to share values between components without explicitly passing props through every level of the component tree.
-
Error Boundaries: Components that catch JavaScript errors anywhere in their child component tree and display fallback UI instead of crashing.
-
Server-Side Rendering (SSR): Enables rendering React components on the server before sending HTML to the client, improving performance and SEO.
-
Concurrent Mode: A set of new features (in development) that help React apps stay responsive and gracefully adjust to the user’s device capabilities and network speed.
-
React Server Components: A new feature that allows components to be rendered entirely on the server, reducing bundle size and improving performance.
-
Suspense: A feature that lets your components “wait” for something before rendering, supporting code-splitting and data fetching with cleaner code.
These features collectively make React powerful for building everything from small widgets to complex, large-scale web applications.
What is JSX?
JSX stands for JavaScript XML and it is an XML-like syntax extension to ECMAScript. Basically it just provides the syntactic sugar for the React.createElement(type, props, ...children) function, giving us expressiveness of JavaScript along with HTML like template syntax.
In the example below, the text inside <h1> tag is returned as JavaScript function to the render function.
export default function App() { return <h1 className="greeting">{"Hello, this is a JSX Code!"}</h1>;}If you don’t use JSX syntax then the respective JavaScript code should be written as below,
import { createElement } from "react";
export default function App() { return createElement( "h1", { className: "greeting" }, "Hello, this is a JSX Code!" );}See class-based version
class App extends React.Component { render() { return <h1 className="greeting">{"Hello, this is a JSX Code!"}</h1>; }}Note: JSX is stricter than HTML
What is the difference between an Element and a Component?
Element:
-
A React Element is a plain JavaScript object that describes what you want to see on the UI. It represents a DOM node or a component at a specific point in time.
-
Elements are immutable: once created, you cannot change their properties. Instead, you create new elements to reflect updates.
-
Elements can be nested within other elements through their
props. -
Creating an element is a fast, lightweight operation-it does not create any actual DOM nodes or render anything to the screen directly.
Example (without JSX):
const element = React.createElement("button", { id: "login-btn" }, "Login");Equivalent JSX syntax:
<button id="login-btn">Login</button>The object returned by
React.createElement:{type: 'button',props: {id: 'login-btn',children: 'Login'}}Elements are then passed to the React DOM renderer (e.g.,
ReactDOM.render()), which translates them to actual DOM nodes.
Component:
-
A Component is a function or class that returns an element (or a tree of elements) to describe part of the UI. Components can accept inputs (called props) and manage their own state (in case of class or function components with hooks).
-
Components allow you to split the UI into independent, reusable pieces, each isolated and composable.
-
You can define a component using a function or a class:
Example (Function Component with JSX):
const Button = ({ handleLogin }) => (<button id="login-btn" onClick={handleLogin}>Login</button>);When JSX is compiled, it’s transformed into a tree of
React.createElementcalls:const Button = ({ handleLogin }) =>React.createElement("button",{ id: "login-btn", onClick: handleLogin },"Login");
In summary:
- Elements are the smallest building blocks in React-objects that describe what you want to see.
- Components are functions or classes that return elements and encapsulate logic, structure, and behavior for parts of your UI.
Think of elements as the instructions for creating UI, and components as reusable blueprints that combine logic and structure to generate those instructions.
How do you create components in React?
Components are the building blocks of creating User Interfaces(UI) in React. There are two possible ways to create a component.
-
Function Components: This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as the one and only one parameter and return React elements to render the output:
function Greeting({ message }) {return <h1>{`Hello, ${message}`}</h1>;} -
Class Components: You can also use ES6 class to define a component. The above function component can be written as a class component:
class Greeting extends React.Component {render() {return <h1>{`Hello, ${this.props.message}`}</h1>;}}
When to use a Class Component over a Function Component?
After the addition of Hooks(i.e. React 16.8 onwards) it is always recommended to use Function components over Class components in React. Because you could use state, lifecycle methods and other features that were only available in class component present in function component too.
But even there are two reasons to use Class components over Function components.
- If you need a React functionality whose Function component equivalent is not present yet, like Error Boundaries.
- In older versions, If the component needs state or lifecycle methods then you need to use class component.
So the summary to this question is as follows:
Use Function Components:
- If you don’t need state or lifecycle methods, and your component is purely presentational.
- For simplicity, readability, and modern code practices, especially with the use of React Hooks for state and side effects.
Use Class Components:
- If you need to manage state or use lifecycle methods.
- In scenarios where backward compatibility or integration with older code is necessary.
Note: You can also use reusable react error boundary third-party component without writing any class. i.e, No need to use class components for Error boundaries.
The usage of Error boundaries from the above library is quite straight forward.
Note when using react-error-boundary: ErrorBoundary is a client component. You can only pass props to it that are serializable or use it in files that have a
"use client";directive.
"use client";
import { ErrorBoundary } from "react-error-boundary";
<ErrorBoundary fallback={<div>Something went wrong</div>}> <ExampleApplication /></ErrorBoundary>;What are Pure Components?
Pure components are the components which render the same output for the same state and props. In function components, you can achieve these pure components through memoized React.memo() API wrapping around the component. This API prevents unnecessary re-renders by comparing the previous props and new props using shallow comparison. So it will be helpful for performance optimizations.
But at the same time, it won’t compare the previous state with the current state because function component itself prevents the unnecessary rendering by default when you set the same state again.
The syntactic representation of memoized components looks like below,
const MemoizedComponent = memo(SomeComponent, arePropsEqual?);Here is the example of how child component(i.e., EmployeeProfile) prevents re-renders for the same props passed by parent component(i.e.,EmployeeRegForm).
import { memo, useState } from "react";
const EmployeeProfile = memo(function EmployeeProfile({ name, email }) { return ( <> <p>Name:{name}</p> <p>Email: {email}</p> </> );});export default function EmployeeRegForm() { const [name, setName] = useState(""); const [email, setEmail] = useState(""); return ( <> <label> Name:{" "} <input value={name} onChange={(e) => setName(e.target.value)} /> </label> <label> Email:{" "} <input value={email} onChange={(e) => setEmail(e.target.value)} /> </label> <hr /> <EmployeeProfile name={name} /> </> );}In the above code, the email prop has not been passed to child component. So there won’t be any re-renders for email prop change.
In class components, the components extending React.PureComponent instead of React.Component become the pure components. When props or state changes, PureComponent will do a shallow comparison on both props and state by invoking shouldComponentUpdate() lifecycle method.
Note: React.memo() is a higher-order component.
What is state in React?
State of a component is an object that holds some information that may change over the lifetime of the component. The important point is whenever the state object changes, the component re-renders. It is always recommended to make our state as simple as possible and minimize the number of stateful components.
Let’s take an example of User component with message state. Here, useState hook has been used to add state to the User component and it returns an array with current state and function to update it.
import { useState } from "react";
function User() { const [message, setMessage] = useState("Welcome to React world");
return ( <div> <h1>{message}</h1> </div> );}Whenever React calls your component or access useState hook, it gives you a snapshot of the state for that particular render.
See class-based version
import React from "react";class User extends React.Component { constructor(props) { super(props);
this.state = { message: "Welcome to React world", }; }
render() { return ( <div> <h1>{this.state.message}</h1> </div> ); }}State is similar to props, but it is private and fully controlled by the component ,i.e., it is not accessible to any other component till the owner component decides to pass it.
What are props in React?
Props are inputs to components. They are single values or objects containing a set of values that are passed to components on creation similar to HTML-tag attributes. Here, data is passed down from a parent component to a child component.
The primary purpose of props in React is to provide following component functionality:
- Pass custom data to your component.
- Trigger state changes.
- Use via
this.props.reactPropinside component’srender()method.
For example, let’s create an element with reactProp property:
<Element reactProp={"1"} />This reactProp (or whatever you came up with) attribute name then becomes a property attached to React’s native props object which originally already exists on all components created using React library.
props.reactProp;For example, the usage of props in function component looks like below:
import React from "react";import ReactDOM from "react-dom";
const ChildComponent = (props) => { return ( <div> <p>{props.name}</p> <p>{props.age}</p> <p>{props.gender}</p> </div> );};
const ParentComponent = () => { return ( <div> <ChildComponent name="John" age="30" gender="male" /> <ChildComponent name="Mary" age="25" geneder="female" /> </div> );};The properties from props object can be accessed directly using destructing feature from ES6 (ECMAScript 2015). It is also possible to fallback to default value when the prop value is not specified. The above child component can be simplified like below.
const ChildComponent = ({ name, age, gender = "male" }) => { return ( <div> <p>{name}</p> <p>{age}</p> <p>{gender}</p> </div> );};Note: The default value won’t be used if you pass null or 0 value. i.e, default value is only used if the prop value is missed or undefined value has been passed.
See class-based version
The Props accessed in Class Based Component as below
import React from "react";import ReactDOM from "react-dom";
class ChildComponent extends React.Component { render() { return ( <div> <p>{this.props.name}</p> <p>{this.props.age}</p> <p>{this.props.gender}</p> </div> ); }}
class ParentComponent extends React.Component { render() { return ( <div> <ChildComponent name="John" age="30" gender="male" /> <ChildComponent name="Mary" age="25" gender="female" /> </div> ); }}What is the difference between state and props?
In React, both state and props are plain JavaScript objects, but they serve different purposes and have distinct behaviors:
State
- Definition:
State is a data structure that is managed within a component. It represents information that can change over the lifetime of the component. - Mutability:
State is mutable, meaning it can be changed using the setter function (setStatein class components or the updater function fromuseStatein functional components). - Scope:
State is local to the component where it is defined. Only that component can modify its own state. - Usage:
State is typically used for data that needs to change in response to user actions, network responses, or other dynamic events. - Re-rendering:
Updating the state triggers a re-render of the component and its descendants.
Props
- Definition:
Props (short for “properties”) are inputs to a component, provided by its parent component. - Mutability:
Props are read-only. A component cannot modify its own props; they are immutable from the component’s perspective. - Scope:
Props are used to pass data and event handlers down the component tree, enabling parent components to configure or communicate with their children. - Usage:
Props are commonly used to make components reusable and configurable. They allow the same component to be rendered with different data or behavior. - Analogy:
Think of props as arguments to a function, whereas state is like variables declared inside the function.
Summary Table
| Feature | State | Props |
|---|---|---|
| Managed by | The component itself | Parent component |
| Mutable | Yes | No (read-only) |
| Scope | Local to the component | Passed from parent to child |
| Usage | Manage dynamic data and UI changes | Configure and customize component |
| Update | Using setState/useState | Cannot be updated by the component |
What is the difference between createElement and cloneElement?
Both React.createElement and React.cloneElement are used to work with React elements, but they serve different purposes.
createElement:
Creates a new React element from scratch. JSX elements will be transpiled to React.createElement() functions to create React elements which are going to be used for the object representation of UI.
Syntax:
React.createElement(type, props, ...children)Example:
React.createElement('button', { className: 'btn' }, 'Click Me')cloneElement:
The cloneElement method is used to clone an existing React element and optionally adds or overrides props.
Syntax:
React.cloneElement(element, newProps, ...children)Example:
const button = <button className="btn">Click Me</button>;const cloned = React.cloneElement(button, { className: 'btn-primary' });// Result: <button className="btn-primary">Click Me</button>How do you write comments in React?
The comments in React/JSX are similar to JavaScript Multiline comments but are wrapped in curly braces.
Single-line comments:
<div> {/* Single-line comments(In vanilla JavaScript, the single-line comments are represented by double slash(//)) */} {`Welcome ${user}, let's play React`}</div>Multi-line comments:
<div> {/* Multi-line comments for more than one line */} {`Welcome ${user}, let's play React`}</div>You can use // and /* */ in JS logic, hooks, and functions.
What are the advantages of React?
Here are the list of main advantages of React,
- Increases the application’s performance with Virtual DOM.
- JSX makes code easy to read and write.
- It renders both on client and server side (SSR).
- Easy to integrate with frameworks (Angular, Backbone) since it is only a view library.
- Easy to write unit and integration tests with tools such as Jest.
What are the limitations of React?
Apart from the advantages, there are few limitations of React too,
- React is just a view library, not a full framework.
- There is a learning curve for beginners who are new to web development.
- Integrating React into a traditional MVC framework requires some additional configuration.
- The code complexity increases with inline templating and JSX.
- Too many smaller components leading to over engineering or boilerplate.
What are the recommended ways for static type checking?
Normally we use PropTypes library (React.PropTypes moved to a prop-types package since React v15.5) for type checking in the React applications. For large code bases, it is recommended to use static type checkers such as Flow or TypeScript, that perform type checking at compile time and provide auto-completion features.
What is the use of react-dom package?
The react-dom package provides DOM-specific methods that can be used at the top level of your app. Most of the components are not required to use this module. Some of the methods of this package are:
render()hydrate()unmountComponentAtNode()findDOMNode()createPortal()
What is ReactDOMServer?
The ReactDOMServer object enables you to render components to static markup (typically used on node server). This object is mainly used for server-side rendering (SSR). The following methods can be used in both the server and browser environments:
renderToString()renderToStaticMarkup()
For example, you generally run a Node-based web server like Express, Hapi, or Koa, and you call renderToString to render your root component to a string, which you then send as response.
// using Expressimport { renderToString } from "react-dom/server";import MyPage from "./MyPage";
app.get("/", (req, res) => { res.write( "<!DOCTYPE html><html><head><title>My Page</title></head><body>" ); res.write('<div id="content">'); res.write(renderToString(<MyPage />)); res.write("</div></body></html>"); res.end();});How do you use innerHTML in React?
The dangerouslySetInnerHTML attribute is React’s replacement for using innerHTML in the browser DOM. Just like innerHTML, it is risky to use this attribute considering cross-site scripting (XSS) attacks. You just need to pass a __html object as key and HTML text as value.
In this example MyComponent uses dangerouslySetInnerHTML attribute for setting HTML markup:
function createMarkup() { return { __html: "First · Second" };}
function MyComponent() { return <div dangerouslySetInnerHTML={createMarkup()} />;}Why should component names start with capital letter?
If you are rendering your component using JSX, the name of that component has to begin with a capital letter otherwise React will throw an error as an unrecognized tag. This convention is because only HTML elements and SVG tags can begin with a lowercase letter.
function SomeComponent { // Code goes here}You can define function component whose name starts with lowercase letter, but when it’s imported it should have a capital letter. Here lowercase is fine:
function myComponent { render() { return <div />; }}
export default myComponent;While when imported in another file it should start with capital letter:
import MyComponent from "./myComponent";Are custom DOM attributes supported in React v16?
Yes. In the past, React used to ignore unknown DOM attributes. If you wrote JSX with an attribute that React doesn’t recognize, React would just skip it.
For example, let’s take a look at the below attribute:
<div mycustomattribute={"something"} />Would render an empty div to the DOM with React v15:
<div />In React v16 any unknown attributes will end up in the DOM:
<div mycustomattribute="something" />This is useful for supplying browser-specific non-standard attributes, trying new DOM APIs, and integrating with opinionated third-party libraries.
What is the difference between React and ReactDOM?
The react package contains React.createElement(), React.Component, React.Children, and other helpers related to elements and component classes. You can think of these as the isomorphic or universal helpers that you need to build components. The react-dom package contains ReactDOM.render(), and in react-dom/server we have server-side rendering support with ReactDOMServer.renderToString() and ReactDOMServer.renderToStaticMarkup().
Why ReactDOM is separated from React?
The React team worked on extracting all DOM-related features into a separate library called ReactDOM. React v0.14 is the first release in which the libraries are split. By looking at some of the packages, react-native, react-art, react-canvas, and react-three, it has become clear that the beauty and essence of React has nothing to do with browsers or the DOM.
To build more environments that React can render to, React team planned to split the main React package into two: react and react-dom. This paves the way to writing components that can be shared between the web version of React and React Native.
Why can’t you update props in React?
The React philosophy is that props should be immutable(read only) and top-down. This means that a parent can send any prop values to a child, but the child can’t modify received props.
What is the reason behind multiple JSX tags to be wrapped?
Behind the scenes, JSX is transformed into plain javascript objects. It is not possible to return two or more objects from a function without wrapping into an array. This is the reason you can’t simply return two or more JSX tags from a function without wrapping them into a single parent tag or a Fragment.