React

 

all about React

The fundamentals and the path to learn



The ability to build stuff in React is one of the hottest skills to have today in software engineering. There is a lot of demand for React developers among startups as well as MNCs.

So, what exactly is React?

Before we get started, let’s be clear with the three terms we come across in this space- React, React.js, and React Native.

React.js or React in general can be considered an open-source JavaScript library that is used to create a user interface (UI) for web applications. React Native on the other hand is used for building cross-platform mobile applications. The underlying framework is more or less the same as React but instead of using web components, it uses native mobile components.

Jordan Walke, then Facebook software engineer, was one of the first to build React. Developers may use React to build web apps that can modify data without refreshing the page.

The major goal for React is to be quick, scalable, and easy to use.

React.js is the most popular web framework as of 2021. (Image Source: Statista)

Why React?

The creation of massive applications with temporal data changes is the fundamental difficulty that React solves. The data in these apps isn’t static, it evolves over time. So, using React you have a better way to display data that is continually changing in an application.

React may be thought of as the V layer in the model-view-controller (MVC) software development pattern. You divide your application into three pieces in the MVC approach. In a database, the model implements data storage and retrieval. The view is responsible for displaying the user interface, whereas the controller is in charge of receiving user input and passing it on to the model. React just allows you to utilize the view component; you’ll need to use different tools for the model and controller sections.

There are some advantages that made React.js so popular:

  • It is straightforward. You only need to describe the app’s look, and React will handle the user interface updates as the data changes.
  • It is declarative in nature. When data changes, React understands how to update the changes.
  • React is a component-based framework. This facilitates the creation of testable, reusable code. You create a component once and reuse it anywhere, any number of times.

Apart from the advantages listed above, React.js should be preferred over a framework like Angular because:

  • It works nicely with component-based user interfaces.
  • Data binding on a single level.
  • Building blocks with a lot of flexibility.
  • JavaScript that is isomorphic.
  • There is a lot of support in the community.
  • In a single location, you can see and control everything.
  • Aids in the development of huge applications.

How to use React?

In React.js you can code without any restrictions, as in there is no architecture reinforcement. However, this can be a disadvantage as you have to look to define your ‘M’ and ‘C’. Else, you are going to end up with some yucky React code. However, most of the time you don’t need controllers as react already has libraries to manage your components state (data) like REDUX, MOBX, ALT, etc. In React it is easier to make smaller web apps without the need for any architecture. However, in bigger apps you need it. You begin with structuring higher-order components first then you plan on holding your component state together with Redux.

You can start a new React project by following some simple steps as per the documentation and you are good to build on top of the starter project.

How does React work?

Everything in React is an “element” or a “component”. Elements are created using something called “JSX” or syntactic javascript. It is basically like declaring HTML content as consts, variables, functions, etc. JSX isn’t absolutely necessary but is generally preferred while coding a React app. Components are like the building blocks of any React app. They may have several elements within them that are programmed to interact and behave in a certain manner.

Every React component has a lifecycle that you can track and alter throughout the course of its three phases:

  1. Mounting
  2. Updating
  3. Unmounting

The term “mounting” refers to the process of inserting components into the Document Object Model (DOM). DOM is basically a standard for how data is represented and accessed on a web page. You can think of it as a tree of HTML/XML objects, which you can somewhat visualize in your developer tools for any web page. Whenever a webpage is loaded the browser creates this DOM with all the content related to that page. Using javascript, you can manipulate and modify these contents. So, when a React app is loaded, the first step is mounting where the components are put into the DOM. React is able to do all this by creating a virtual DOM which is then used to update and sync with the real DOM.

When mounting a component, React calls the following four built-in functions in this order:

  1. constructor()- This is the first step where initial state values are set.
  2. getDerivedStateFromProps()- The getDerivedStateFromProps() method is called right before rendering the element(s) in the DOM.
  3. render()- This is the method that outputs the HTML to the DOM.
  4. componentDidMount()- The componentDidMount() method is called after the component is rendered.

The render() method is essential and will be called at all times; the others are optional and will be invoked only if they are defined.

When a component is updated, the lifecycle moves on to the next step. When the state or props (shorthand for properties) of a component change, the component is said to be updated. To do the same, React calls the following five built-in methods in this order:

  1. getDerivedStateFromProps()
  2. shouldComponentUpdate()
  3. render()
  4. getSnapshotBeforeUpdate()
  5. componentDidUpdate()

Each of these methods helps determine and update the state changes in the component and re-render them. In the next phase of the lifecycle, the component gets removed from the DOM or unmounted as React likes to call it. When a component is unmounted, just one built-in method in React is called:

  1. componentWillUnmount()

The developers can write the logic in these methods to modify the behavior of the React app as and when they are called.

Where is React used in real world?

React has played a significant part in providing a digital experience to Instagram users. In terms of UI and UX, the app has a fantastic appearance and feel. It’s because to React Native that you can now experience fantastic UI and UX.

When it comes to utilizing social media on a regular basis, Whatsapp has officially published ReactJS for creating user interfaces from Facebook.

Myntra is a major fashion e-commerce company in India. The best user experience you receive is the right appearance and feel is because of the help of the React-based mobile app.

React is also extensively used by many MNCs and startups as part of their front-end stack for both internal tools and consumer/business-facing applications. It is generally coupled with other frameworks like Redux or Next.Js.

Why you may not want to use React.js for complex interactive front-end projects?

  • React.js components are difficult to reuse in complex interactive web projects.
  • React.js Virtual DOM algorithm is time-consuming and imprecise.
  • React.js HTML templates are neither complete nor powerful.

0 Comments