FrontendDeveloper.in

React Interview Questions

  • Question 1

    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.

  • Question 2

    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.

  • Question 3

    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.

  • Question 4

    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

    class App extends React.Component {
    render() {
    return <h1 className="greeting">{"Hello, this is a JSX Code!"}</h1>;
    }
    }
    

    Note: JSX is stricter than HTML

  • Question 5

    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.createElement calls:

    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.

  • Question 6

    How to 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.

    1. 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>;
    }
    
    1. 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>;
    }
    }
    
  • Question 7

    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.

    1. If you need a React functionality whose Function component equivalent is not present yet, like Error Boundaries.
    2. 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>;
    
  • Question 8

    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?);
    

    Below 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 (
    <>
    </>
    );
    });
    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.

  • Question 9

    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.

    state

    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 (
    <h1>{message}</h1>
    );
    }
    

    Whenever React calls your component or access useState hook, it gives you a snapshot of the state for that particular render.

    See Class

    import React from "react";
    class User extends React.Component {
    constructor(props) {
    super(props);
    
    this.state = {
    message: "Welcome to React world",
    };
    }
    
    render() {
    return (
    <h1>{this.state.message}</h1>
    );
    }
    }
    

    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.

  • Question 10

    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, the 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:

    1. Pass custom data to your component.
    2. Trigger state changes.
    3. Use via this.props.reactProp inside component's render() method.

    For example, let us 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 (
    );
    };
    
    const ParentComponent = () => {
    return (
    <ChildComponent name="John" age="30" gender="male" />
    <ChildComponent name="Mary" age="25" geneder="female" />
    );
    };
    

    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 (
    );
    };
    

    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

    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 (
    );
    }
    }
    
    class ParentComponent extends React.Component {
    render() {
    return (
    <ChildComponent name="John" age="30" gender="male" />
    <ChildComponent name="Mary" age="25" gender="female" />
    );
    }
    }
    
  • Question 12

    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 (setState in class components or the updater function from useState in 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.
  • Question 13

    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.
  • Question 14

    Summary Table

    FeatureStateProps
    Managed byThe component itselfParent component
    MutableYesNo (read-only)
    ScopeLocal to the componentPassed from parent to child
    UsageManage dynamic data and UI changesConfigure and customize component
    UpdateUsing setState/useStateCannot be updated by the component

  • Question 15

    What is the difference between HTML and React event handling?

    Below are some of the main differences between HTML and React event handling,

    1. In HTML, the event name usually represents in lowercase as a convention:
    <button onclick="activateLasers()"></button>
    

    Whereas in React it follows camelCase convention:

    <button onClick={activateLasers}>
    
    1. In HTML, you can return false to prevent default behavior:
    <a
    href="#"
    onclick='console.log("The link was clicked."); return false;'
    />
    

    Whereas in React you must call preventDefault() explicitly:

    function handleClick(event) {
    event.preventDefault();
    console.log("The link was clicked.");
    }
    
    1. In HTML, you need to invoke the function by appending () Whereas in react you should not append () with the function name. (refer "activateLasers" function in the first point for example)
Get LinkedIn Premium at Rs 399