Professional Documents
Culture Documents
What Is The Virtual DOM?: Reconciliation
What Is The Virtual DOM?: Reconciliation
What Is The Virtual DOM?: Reconciliation
What’s React?
As we said earlier React.js is JavaScript library for building user interfaces.
But, what is a library in reality?
A library is sets of function and classes that are grouped in a package. The main idea behind
libraries is to create a reusable code that any one in the development community can use it and
contribute to it.
What’s React?
If you type “What’s React.js?” in google you will find thousands of similar articles talking about
React.js. But let’s make it simple.
React.js is an open-source JavaScript library that is used for building user interfaces specifically for
Single Page Applications.
It’s handling the view layer for web and mobile applications.
Features of react
• JSX: React uses the JSX syntax in writing it’s component which make the component more
independent.
• Component: React adopte the component based application approach which allows us to
use the same component as many times as we need.
• One way data flow: React only allows us to pass data from parent to child, which is very
helpful to trace data when debugging.
• Virtual DOM: React uses the Virtual DOM which makes rendering UI super fast.
• Simplicity: React is very simple to learn and work with especially for newcomers.
npx
Now, we are going to create our first React project.
React projects often requires a couple of packages and configuration. Fortunately, there is a npm
package called create-react-app that takes care of all that.
We can call this package directly without installing it thanks to npx command.
Create-react-app
Now, we will create our project using create-react-app and npx command.
Here are the steps to do that:
1. Let’s create a folder for the React projects. You can choose any empty folder.
2. Access it using the Command line/ Terminal.
3. Run the following command to create the React project:
npx create-react-app <project-name>
Note: Replace the <project-name> with your project name
This command will create a subfolder named after the project name you just specified.
Start a project
Let’s run the project and see how our React app looks like. First we need to:
1. Access the subfolder that was created by create-react-app using the Command line/
Terminal.
2. Run the following command:
Desktop> cd myfirstapp
Desktop/myfirstapp> npm start
Tip: This command will run the script named “start” in the package.json
he previous command will open the browser containing our React website.
Now, try to play around with the html that is inside the App.js file that you can find in the src
folder.
Make sure you have a backup of the App.js file.
The project structure:
You might be wondering why the React project contains a lot of folders and files. We won’t go
through all of them, but we must understand the purpose of some of them.
We can split these files into two sections.
1. npm related files
• node_modules
• package.json
2. React related files
• public
• src
• Node_modules: A folder contains all of the dependencies installed in the project (including
React).
Tip: If we accidently delete the node_modules, we can always reinstall the packages from the
package.json.
the package.json keeps track of all the packages we used. In fact, when sharing projects, we
exclude the node_modules folder.
React files
• Public folder: where we are going to put all of our images, SVGs, icons and all the assets
that we need for our project.
This folder contains an index.html. This file is the first and only HTML page that gets
loaded when we access to our website. It’s the entry point to our project (it’s a single-page
application, do your remember?).
• Source folder: the src folder contains our code. We will be most of the time working on this
directory.
What npm does ?Keep track of the modules names used along with their versions.Installs
dependencies for us so we don’t need to include them one by one.Makes sharing projects easier,
since we don’t have to include installed modules (node_modules folder) in our project.
The package.json contain:
Introduction
To understand how the concept of virtual DOM was born, let's return briefly to the DOM.
This document can be represented by the following DOM tree:
The V.DOM
React maintains two Virtual DOM at each time, one contains the updated Virtual DOM and one
which is just the pre-update version of this updated Virtual DOM.
Now it compares the pre-update version with the updated Virtual DOM and figures out what exactly
has changed in the DOM.
This process is known as ‘diffing’. Once React finds out what exactly has changed then it updated
those objects only, on real DOM.
This significantly improves the performance and is the main reason why Virtual DOM is much
loved by developers all around.
ReactDom package
ReactDOM is a package that provides DOM specific methods, it can be used at the top level of a
web app to enable an efficient way of managing DOM elements of the web page.
react-dom
The render method accepts:
3 arguments
REACT JS RECAP
Front-end ecosystem is constantly evolving and changing on a day-to-day basis. Some tools become
“bestsellers” in terms of web app development, revolutionizing the workflow, while others become
a dead end.
After this Super Skill, we do all agree that React is a bestseller and the reasons are numerous :
• facilitate coding
• boosts productivity
• facilitate testing
• guarantees stable code
• creates user friendly interfaces
To summarize React is the key to build large-scale applications with data that changes repeatedly
over time