Sunday, 15 May 2022

React JS Comments

 Creating a new application

Create React App provides multiple ways to create React application.

Using npx script.

npx create-react-app <react-app-name>

npx create-react-app hello-react-app

Using npm package manager.

npm init react-app <react-app-name>

npm init react-app hello-react-app

Using yarn package manager.

yarn init react-app <react-app-name>

yarn init react-app hello-react-app

Selecting a template

Create React App creates React application using default template. Template refers the initial code with certain build-in functionality. There are hundreds of template with many advanced features are available in npm package server. Create React App allows the users to select the template through –template command line switch.

create-react-app my-app --template typescript

Installing a dependency

Using npm package manager.

npm install --save react-router-dom

Using yarn package manager.

yarn add react-router-dom


Running the application

React application can be started using npm or yarn command depending on the package manager used in the project.

Using npm package manager.

npm start

Using yarn package manager.

yarn start

To run the application in secure mode (HTTPS), set an environment variable, HTTPS and set it to true before starting the application. For example, in windows command prompt (cmd.exe), the below command set HTTPS and starts the application is HTTPS mode.

cmd

set HTTPS=true && npm start


npm start and  npm run are same further check with below link

https://docs.npmjs.com/cli/v8/commands/npm-run-script

Building

A single command is enough to create a production build of the application.

npm run build

> expense-manager@0.1.0 build path\to\expense-manager

> react-scripts build

Creating an optimized production build...

Compiled with warnings.

File sizes after gzip:

   41.69 KB   build\static\js\2.a164da11.chunk.js

    2.24 KB   build\static\js\main.de70a883.chunk.js

    1.4  KB   build\static\js\3.d8a9fc85.chunk.js

    1.17 KB   build\static\js\runtime-main.560bee6e.js

  493     B   build\static\css\main.e75e7bbe.chunk.css


The project was built assuming it is hosted at /.

You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.

You may serve it with a static server:

   npm install -g serve

   serve -s build


Real DOM and Virtual DOM

 What is DOM?

“The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.”

The DOM is an abstraction of a page’s HTML structure. It takes HTML elements and wraps them in an object with a tree-structure — maintaining the parent/child relationships of those nested HTML elements. This provides an API that allows us to traverse nodes (HTML elements) and manipulate them in a number of ways — such as adding nodes, removing nodes, editing a node’s content, etc.


Real DOMVirtual DOM
DOM is a language-neutral interface allowing programs and scripts to dynamically access and update multiple objects like content, structure, and style of a document.Is a collection of modules designed to provide a declarative way to represent the DOM for an application.
The DOM represents the document as nodes and objects.A virtual DOM object is a representation of a DOM object, like a lightweight copy.
It is an object-oriented representation of a web page, modified with a scripting language like JavaScript.Virtual DOM is ideal for mobile-first applications.





How does updates work in React?

On the first load, ReactDOM.render() will create the Virtual DOM tree and real DOM tree.

As React works on Observable patterns, when any event(like key press, left click, api response, etc.) occurred, Virtual DOM tree nodes are notified for props change, If the properties used in that node are updated, the node is updated else left as it is.

React compares Virtual DOM with real DOM and updates real DOM. This process is called Reconciliation. React uses Diffing algorithm techniques of Reconciliation.

Updated real DOM is repainted on browser