React.js Introduction

Last updated on

In this introduction, you will understand what React JS is and learn how to use NPM commands to get started with React, such as install and start.

React is a popular tool for building UIs (user interfaces) on the web. If you are new to web development, you may wonder what React is and how it can help you create better user experiences.

So, in the following section, you will understand what react is. Let’s get started.

Introduction to React.js Definition

A JavaScript Library React JS (also known as React) which is mostly used in front-end web development.

It makes designing interactive user interfaces more simple whereas page rendering with dynamic and data makes HTML elements quite easy. React, developed and maintained by the Facebook team lets you develop large web applications that can change data without reloading

At the core of React is its ability to divide complex user interfaces into small, reusable parts. That allows for more efficient and maintainable development.

React.js follows the declarative programming model, where developers describe what should happen with a user interface. And a conditional from React that deals with the rendering logic.

The main reason for this is a vDOM with React to keep track of the changes in your app’s state. So it can update only what needs updating and avoid expensive manipulation directly on the browser resulting in more performance.

Lots of people use and support React. It’s free and open for anyone to use. That means a lot of smart developers around the world help make it better.

Let’s take a brief look at the history.

History and Background

Jordan Walke, a software engineer at Facebook is the person who developed React JS first in 2011. It was developed due to the increasing complexity of Facebook’s dynamic user interfaces and traditional JavaScript frameworks couldn’t handle these frequent modifications, with framework inefficiency at processing data.

Initially, React.js originated at Facebook and was first used internally in its newsfeed before the realization of what it could be. The React library was open-sourced by 2013 when Facebook released it as a JSConf US conference. This made the wider developer community able to partake in developing new functions for this library it has gained popularity quickly due to its different way of thinking about UI development.

Over the years, React has evolved a lot and it receives many updates with various improvements. Such as new features like hooks — that allow functional components to manage state and side effects. Or even an internal rewrite of its core algorithm called Fiber which aims for more performant rendering.

Today it is one of the few and most popular libraries in Front-end development! It has been since used by individuals from all around the world to create some marvelous websites, not only that even leading Organizations like Facebook, Instagram Airbnb Netflix started using React which makes it a must-have tool for any web-developer right now.

In the following section, you will understand how the virtual dom works in React.js.

Understanding the Virtual DOM

The virtual dom is one of the most ingenious ideas that have made React super fast and powerful for developing dynamic user interfaces. That is one of the basic concepts React uses to optimize app performance—allowing developers to build very responsive applications, without worrying about it being slow or inefficient.

The Virtual DOM (VDOM) is simply an in-memory representation of actual DOM elements. It is a lightweight copy of the actual DOM, providing React the ability to do fast updates with minimal direct manipulation which can be slow and computationally expensive.

Here’s how the Virtual DOM works in the React.js application:

  1. Initial Rendering.
  2. Updating the UI.
  3. Diffing Algorithm.
  4. Reconciliation.
  5. Batching Updates.

Let’s see each one of them in detail.

Initial Rendering

React will build a Virtual DOM tree when the component is first rendered. This is a representation in memory of the VDOM structure which has a form similar to real DOM. This VDOM is used by React.js to manage and track the changes in UI.

Updating the UI

As soon as some state or props get changed for a component then react re-render the components which generates a new Virtual DOM tree. React does not update the real DOM right away; instead, it compares the new Virtual DOM tree with the previous one and finds out what has changed. This operation is also known as “diffing”.

Diffing Algorithm

This is when two lists of elements are compared and the minimum number of operations required to adjust a list can be applied as mutations on an actual DOM. Virtual DOM will compare the elements in both Virtual DOM trees and identify which exact element has been changed, removed, or added.

Reconciliation

This is where react compares the virtual and real DOM to learn what has changed, so it can propagate the necessary updates without you telling it how or re-rendering everything all out. This process is called Reconciliation which makes UI to be updated efficiently and quickly.

Batching Updates

It batch updates together for optimized rendering performance. Instead of touching the real DOM for every single change that is made, React holds changes and updates a virtual DOM layer inside until at some points determined to make all updates in one-time batch mode reducing touching the visual DOM.

Another key concept in React is the use of JSX (JavaScript XML), which allows you to write HTML-like syntax directly in your JavaScript code. JSX is not required to use React, but it makes it easier to create and manage complex UIs.

Anyway, in the following section, you will learn how to write an application using React.js.

Write Your First App with React

To begin, it is necessary to install a recent version of node.js on your computer. To install it, follow the instructions on the official page.

Once installed, you can verify NPM and Node.js by using the command below.

# Verifying Node.js
node -v

# Verifying Node.js
npm -v

If everything is ok, you can start your first app with React.js. Simply run the command below and follow its instructions.

npx create-react-app [react folder name]

After executing this command, you will need to wait for a moment while the folder is being downloaded.

React.js Introduction

If you take a look at this image, you will be able to see the packages that will be installed in the node modules folder, such as:

  • React
  • React DOM
  • React Scripts
  • with many other 209 packages.

And there are some commands that you can use to run your React app.

When you open the project folder, you’ll find two files called “package-lock” and “package.json.” You’ll also see three folders, which are named as follows:

  • The “node_modules” folder.
  • The “src” folder contains jsx, CSS, and so many others
  • the “public” folder displays the compiled file in public access.

These files are all about Webpack, a tool that helps you mix together JavaScript, CSS, and other files and put them into the public folder.

Right now, you don’t need to worry about what’s inside these files. We’ll go over what they do in our next lesson. Let’s concentrate on making your first React app instead.

Anyway, before getting started, you have to learn more about Node NPM.

Let’s start the React application with the following command:

npm run start

Once you run this React app through the previous NPM command, it will open the browser with this URL http://localhost:3000/.

React Official Site

FAQs

What is React.js?

As in the above introduction, React.js is a popular Javascript library for building dynamic user interfaces, particularly slick single-page web applications.

How does React.js help in building user interfaces?

In React reusable components are used when building multiple complex user interfaces ensuring a consistent and easy-to-use experience for the developer.

What is the main benefit of using React’s Virtual DOM?

The Virtual DOM increases performance by updating just what is needed to the actual DOM, rendering as fast as it can.

Who developed React.js and when?

Facebook developed React.js, and it was released in 2013.

What were the reasons behind the development of React.js?

As I mentioned in the above introduction, React.js was created to solve this problem and make our lives easier when building highly scalable web applications.

How has React evolved since its initial release?

Since its release, It has introduced hooks, context API, and performance optimizations, making it more powerful and easier to use.

What are the key features of React.js?

It supports a component-based architecture, Virtual DOM and JSX with unidirectional data flow.

How Does React Work?

When you create the component, you define its behavior by writing code that describes how it should render based on its current state and any incoming data or props. The component’s state is managed by React, and any changes to it trigger a re-rendering of the component and its child components.

One of the key features of React is its virtual DOM (Document Object Model). The virtual DOM is a lightweight representation of the actual DOM, and it allows React to efficiently update the UI without directly manipulating the DOM. When you update the state of a React component, React will compare the new virtual DOM with the previous one and only update the parts of the actual DOM that have changed. This makes React faster and more efficient than other approaches to UI development.

React also provides a unidirectional data flow, meaning that data can only flow in one direction, from parent components down to child components. This makes it easier to manage and debug your application, as you always know where the data is coming from and where it is going.

To get more details about how Virtual DOM works, scroll up into the React.js introduction and read the steps related it in detail.

What is the purpose of the diffing algorithm in React?

Using a diffing algorithm, the Virtual DOM can discover what has changed between renderings and because of this optimization updates are rendered more effectively.

What is the reconciliation process in React.js?

Reconciliation is the process in which React updates RealDOM to be the same as Virtual DOM after change detection.

What is JSX and why is it useful in React development?

JSX is a syntax extension of JavaScript, it makes writing HTML code within the JavaScript file which can be used to create react components.

What steps are involved in creating your first React app?

It includes steps like installing Node.js and starting up the development server of a React application in Node.js using npx create-react-app.

What are the necessary tools to install before starting with React?

Install Node.js, NPM (Node Package Manager), and a code editor like VS Code.

How do you verify the installation of Node.js and NPM?

Check by running node -v and npm -v in the terminal to see which versions installed

What are the contents of a React project’s initial structure?

If we look at the structure, it consists of the node_modules folder (our system), src, public folders, and setup files like package.json.

What is the significance of the node_modules, src, and public folders in a React project?

The dependencies are located inside of the node_modules folder, the source code is found within src, and any static files will be placed in public.

How do you start a React application using NPM?

Start a React app by running the command npm start in the terminal.

What happens when you run a React app using npm run start?

The app will compile, run a development server, and open the browser showing your application immediately updating every time you save.

Let’s summarize it.

Wrapping Up

This Introduction brings forward a well-organized outline of the concepts covering the core functions of the React JS framework. React is really one of the most famous libraries for web user interface design. It is frequently utilized for building effective UIs, with a focus on SPAs. The very first aspect mentioned is the introduction to React. It gives an idea of how the user interface with many interacting parts can be realized in a simpler manner. This is made possible by the use of components. Also, the article brings up the benefits of using React. For example, the Virtual DOM is responsible for re-rendering only the portions of the application that have changed. This functionality enhances the speed of use of applications.

Next in turn the article speaks about installing Node on your home machine. It deals with configuring most of the rest of the environment. Calling npm start, the React setup creates a new application. The article however gives a little description of some crucial documents and directories. It describes node_modules, src, public files, etc. Last but not least, it describes how Webpack helps to keep project files organized.

This introduction also covers an FAQ section of React.js that answers basic questions like the advantages of React, how it works, and important features about the same. The article can be used as a help for beginners who want to understand React JS and start with it.

Thank you for reading. Happy Coding!

Share on: