2022-02-19

ReactJS

What is ReactJS

React, often referred to as React.js or ReactJS, is an open-source JavaScript library that was developed by Facebook. It was first released in 2013, and since then, it has gained immense popularity among developers and companies. The primary purpose of React is to build user interfaces, particularly for SPA.

React allows developers to create complex user interfaces from isolated pieces of code called components. These components can be reused, which helps in maintaining consistency across an application and also makes development more efficient.

One of the standout features of React is its virtual DOM. Traditional web applications would render pages on the server and send the entire HTML and CSS to the client. This approach was inefficient, especially for large applications. React’s virtual DOM optimizes this process by rendering components and only updating the parts of the page that have changed. This results in faster rendering and a smoother user experience.

Another advantage of using React is its extensive community and ecosystem. There is a wealth of libraries, tools, and resources available to React developers. Moreover, because it is maintained by Facebook and other community members, it is constantly being improved and updated.

React is also versatile in the sense that it can be used for the development of both web and mobile applications. React Native, a sibling to ReactJS, allows developers to write mobile applications using React concepts.

Core Concepts

Components

Components are the building blocks of React applications. A component is essentially a self-contained piece of code that represents a part of the user interface (UI). Components can be thought of like LEGO blocks; you can assemble them in different ways to build complex UIs.

There are two main types of components in React:

  • Functional Components
    These are simpler and are used for components that only need to render content and don’t need to manage their state. A functional component is just a JavaScript function that returns JSX.

  • Class Components
    Before React introduced Hooks, class components were used for components that needed to have their own state and lifecycle methods. Class components require more boilerplate code compared to functional components but are powerful and provide more features.

JSX (JavaScript XML)

JSX stands for JavaScript XML. It is a syntax extension for JavaScript, which allows you to write what looks like HTML within your JavaScript code. JSX is not required to build React applications, but it makes the code more readable and expressive. It's important to note that browsers don't understand JSX, so it needs to be compiled into regular JavaScript using a tool like Babel.

jsx
const element = <h1>Hello, World!</h1>;

Virtual DOM

One of the groundbreaking features of React is its implementation of the Virtual DOM. The DOM is a representation of the UI. However, directly manipulating the DOM is slow and inefficient. React creates a virtual representation of the DOM, and whenever a component's state changes, it updates the Virtual DOM. React then compares the Virtual DOM with the actual DOM using a process called “Reconciliation” and efficiently updates only the parts of the UI that have changed. This process is known as “diffing”.

Props and State

Props and State are two concepts that are essential in managing data in a React application.

  • Props
    Props, or properties, are a way of passing data from parent to child components. They are immutable, meaning that a component cannot change its props. They serve as a mechanism to allow components to communicate with each other.

  • State
    State, on the other hand, is used for data that should change over time and re-render the component when it does. State is mutable, and changes to the state trigger a re-render of the component.

Setting Up the Development Environment

Installation and Configuration

Before you can start building applications with React, you need to set up your development environment. This involves installing Node.js and a package manager such as npm or Yarn.

Creating a New Project

Now that your development environment is set up, it’s time to create a new React project. React provides a command-line tool called create-react-app, which sets up a new project with a sensible default configuration.

  1. Create a New React Application

Open your terminal or command prompt and run the following command:

bash
$ npx create-react-app my-react-app

This creates a new directory called my-react-app with all the files and configurations needed for a React application.

  1. Navigate to Your Project Directory

Change to the newly created directory by using the following command:

bash
cd my-react-app
  1. Start the Development Server

Now, let’s start the development server by running the following command:

bash
$ npm start

This command starts a local development server and opens your default web browser to display your new React app. You should see a welcome screen with the React logo.

React

References

https://react.dev/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!