What is React js? Why React js is so popular?

React is a JavaScript library for building user interfaces that was developed by Facebook in 2013. It is often used for building single-page applications and mobile applications, and has become one of the most popular JavaScript libraries in the world. In this article, we’ll explore what React is, how it works, and why it has become so popular among developers.

What is React?

At its core, React is a library for building user interfaces. It allows developers to declaratively describe the structure of their interfaces and the relationships between different pieces of data. This makes it easier to reason about the structure of the interface and manage the data that is displayed to the user.

One of the main benefits of React is that it uses a declarative programming style. This means that developers describe what they want the interface to look like, and React takes care of rendering the appropriate elements to the DOM (the Document Object Model, which represents the structure of an HTML or XML document). This makes it easier for developers to focus on the logic of their application, rather than the details of how to manipulate the DOM.

React also uses a virtual DOM, which is a lightweight in-memory representation of the actual DOM. When a component in a React application is updated, the virtual DOM is updated to reflect the changes. React then uses a diffing algorithm to determine the minimal set of changes that need to be made to the actual DOM in order to reflect the updates in the virtual DOM. This helps to optimize performance by reducing the number of DOM manipulation operations that need to be performed.

Components

In React, a user interface is made up of components. A component is a self-contained piece of code that represents a part of the interface. Components can be reused throughout an application, which makes it easier to build and maintain large, complex interfaces.

There are two types of components in React: functional components and class-based components.

Functional components are simple functions that take in props (short for “properties”) and return a React element. Here is an example of a functional component that displays a message:

import React from 'react';

function HelloMessage(props) {
  return <div>Hello {props.name}</div>;
}

In this example, the HelloMessage component takes in a name prop and renders it inside a div element.

Class-based components, on the other hand, are JavaScript classes that extend the React.Component class. They have a more complex structure than functional components, but offer additional features such as the ability to use state and lifecycle methods. Here is an example of a class-based component that displays a message:

import React from 'react';

class HelloMessage extends React.Component {
  render() {
    return <div>Hello {this.props.name}</div>;
  }
}

In this example, the HelloMessage component takes in a name prop and renders it inside a div element, just like the functional component above. The main difference is that the HelloMessage component is now a class that extends the React.Component class.

Props

As we saw in the examples above, props are used to pass data into a component. Props are read-only, which means that a component cannot modify its own props. This helps to ensure that the data flow in a React application is unidirectional, which makes it easier to reason about the state of the application.