Core React

  1. What is React?

    React (aka React.js or ReactJS) is an open-source front-end JavaScript library that is used for building composable user interfaces, especially for single-page applications. It is used for handling view layer for web and mobile apps based on components in a declarative approach.

    React was created by Jordan Walke, a software engineer working for Facebook. React was first deployed on Facebook's News Feed in 2011 and on Instagram in 2012.

  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.

    Note: JSX comes from the idea of XHP

  3. What are the major features of React?

    The major features of React are:

    • Uses JSX syntax, a syntax extension of JS that allows developers to write HTML in their JS code.
    • It uses Virtual DOM instead of Real DOM considering that Real DOM manipulations are expensive.
    • Supports server-side rendering which is useful for Search Engine Optimizations(SEO).
    • Follows Unidirectional or one-way data flow or data binding.
    • Uses reusable/composable UI components to develop the view.
  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!"
      );
    }
    

    Note: JSX is stricter than HTML

  5. What is the difference between Element and Component?

    An Element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it cannot be mutated.

    The JavaScript representation(Without JSX) of React Element would be as follows:

    const element = React.createElement("div", { id: "login-btn" }, "Login");
    

    and this element can be simiplified using JSX

    <div id="login-btn">Login</div>
    

    The above React.createElement() function returns an object as below:

    {
      type: 'div',
      props: {
        children: 'Login',
        id: 'login-btn'
      }
    }
    

    Finally, this element renders to the DOM using ReactDOM.render().

    Whereas a component can be declared in several different ways. It can be a class with a render() method or it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output:

    const Button = ({ handleLogin }) => (
      <div id={"login-btn"} onClick={handleLogin}>
        Login
      </div>
    );
    

    Then JSX gets transpiled to a React.createElement() function tree:

    const Button = ({ handleLogin }) =>
      React.createElement(
        "div",
        { id: "login-btn", onClick: handleLogin },
        "Login"
      );
    
  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>;
      }
    }
    
  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 serializeable 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>;
    
  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 (
        <>
          <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.

  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.

    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

    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.

  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 (
        <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 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>
        );
    }
    }
    
  11. What is the difference between state and props?

    In React, both state and props are plain JavaScript objects and used to manage the data of a component, but they are used in different ways and have different characteristics.

    The state entity is managed by the component itself and can be updated using the setter(setState() for class components) function. Unlike props, state can be modified by the component and is used to manage the internal state of the component. Moreover, changes in the state trigger a re-render of the component and its children. The components cannot become reusable with the usage of state alone.

    On the otherhand, props (short for "properties") are passed to a component by its parent component and are read-only, meaning that they cannot be modified by the own component itself. Also, props can be used to configure the behavior of a component and to pass data between components. The components become reusable with the usage of props.

  12. 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)
  13. What are synthetic events in React?

    SyntheticEvent is a cross-browser wrapper around the browser's native event. Its API is same as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers. The native events can be accessed directly from synthetic events using nativeEvent attribute.

    Let's take an example of BookStore title search component with the ability to get all native event properties

    function BookStore() {
      function handleTitleChange(e) {
        console.log("The new title is:", e.target.value);
        // 'e' represents synthetic event
        const nativeEvent = e.nativeEvent;
        console.log(nativeEvent);
        e.stopPropagation();
        e.preventDefault();
      }
    
      return <input name="title" onChange={handleTitleChange} />;
    }
    
  14. What are inline conditional expressions?

    You can use either if statements or ternary expressions which are available from JS to conditionally render expressions. Apart from these approaches, you can also embed any expressions in JSX by wrapping them in curly braces and then followed by JS logical operator &&.

    <h1>Hello!</h1>;
    {
      messages.length > 0 && !isLogin ? (
        <h2>You have {messages.length} unread messages.</h2>
      ) : (
        <h2>You don't have unread messages.</h2>
      );
    }
    
  15. What is "key" prop and what is the benefit of using it in arrays of elements?

    A key is a special attribute you should include when mapping over arrays to render data. Key prop helps React identify which items have changed, are added, or are removed.

    Keys should be unique among its siblings. Most often we use ID from our data as key:

    const todoItems = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);
    

    When you don't have stable IDs for rendered items, you may use the item index as a key as a last resort:

    const todoItems = todos.map((todo, index) => (
      <li key={index}>{todo.text}</li>
    ));
    

    Note:

    1. Using indexes for keys is not recommended if the order of items may change. This can negatively impact performance and may cause issues with component state.
    2. If you extract list item as separate component then apply keys on list component instead of li tag.
    3. There will be a warning message in the console if the key prop is not present on list items.
    4. The key attribute accepts either string or number and internally convert it as string type.
    5. Don't generate the key on the fly something like key={Math.random()}. Because the keys will never match up between re-renders and DOM created everytime.
  16. What is Virtual DOM?

    The Virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the "real" DOM. It's a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.

  17. How Virtual DOM works?

  18. What is the difference between Shadow DOM and Virtual DOM?

    The Shadow DOM is a browser technology designed primarily for scoping variables and CSS in web components. The Virtual DOM is a concept implemented by libraries in JavaScript on top of browser APIs.

  19. What is React Fiber?

    Fiber is the new reconciliation engine or reimplementation of core algorithm in React v16. The goal of React Fiber is to increase its suitability for areas like animation, layout, gestures, ability to pause, abort, or reuse work and assign priority to different types of updates; and new concurrency primitives.

  20. What is the main goal of React Fiber?

    The goal of React Fiber is to increase its suitability for areas like animation, layout, and gestures. Its headline feature is incremental rendering: the ability to split rendering work into chunks and spread it out over multiple frames.

    from documentation

    Its main goals are:

    1. Ability to split interruptible work in chunks.
    2. Ability to prioritize, rebase and reuse work in progress.
    3. Ability to yield back and forth between parents and children to support layout in React.
    4. Ability to return multiple elements from render().
    5. Better support for error boundaries.