Unit 1
Unit 1
• Initially, there is an Actual DOM(Real DOM) containing a div with two child
elements: hi and h2.
• React maintains a previous Virtual DOM to track the UI state before any
updates.
2. Detecting Changes
• Instead of updating the entire DOM, React updates only the changed part in the
New Actual DOM, making the update process more efficient.
Key Features of React
React is one of the most demanding JavaScript libraries because it is equipped with
a ton of features which makes it faster and production-ready. Below are the few
features of React.
1. Virtual DOM
React uses a Virtual DOM to optimize UI rendering. Instead of updating the entire
real DOM directly, React:
• Creates a lightweight copy of the DOM (Virtual DOM).
• Compares it with the previous version to detect changes (diffing).
• Updates only the changed parts in the actual DOM (reconciliation), improving
performance.
2. Component-Based Architecture
React manages component state efficiently using the useState hook (for functional
components) or this.state (for class components). State allows dynamic updates
without reloading the page.
• React vs Angular
React Angular
React uses one-way data binding, Angular uses two-way data binding
React Angular
React uses JSX (JavaScript XML) Angular uses HTML templates with special
for templating. Angular directives.
History of React
• It was designed to create dynamic, fast, and responsive user interfaces for
web applications by focusing on the view layer.
• React introduced concepts like components (reusable UI pieces) and the
virtual DOM (a lightweight copy of the real DOM) for optimizing UI
updates.
• React is now a widely used framework for building modern web and
mobile apps, supported by a strong community and major companies.
Platform
iOS, Android (Mobile apps) Web (primarily browsers)
Target
Programmin JavaScript (with JSX), JavaScript, TypeScript
g Language supports TypeScript supported
State
Redux, Context API, MobX Vuex, Pinia
Management
Community
Very large and active Large and growing community
Support
Mobile application
Use Case Web application development
development
Popular I n stagram,
Alibaba, Xiaomi, GitLab
Companies Using Facebook, Airbnb
We must have NodeJS installed on our PC. So, the very first step will
be to install NodeJS. Once we have set up NodeJS on our PC, the next
thing we need to do is set up React Boilerplate.
node_modules v public
# Favicon, ico O index.html
logo19Z,png logo51Z.png l)
maniFest.json =■ robots.txt v
src
# App.css JS App.js
JS App.test.js
# index.css JS index.js *■
logo,svg
JS reportWebVitalsJs Js
setupTestsjs .gitignore
{} package-Lock.json {}
packagejson |® README.md
Application structure
It will install some packages by default which can be seen in the dependencies in
package.json file as follows:
"dependencies": {
"@testing-library/jest-dom": "A5.17.0",
"@testing-library/react": 'A 3.4.0",
"@testing-library/user-event": 'A 3.5.0",
"react": "A18.2.0",
"react-dom": "A18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "A2.1.4"
}
If you are using npm version 5.1 or less, you cannot use the npx command, you
need to install the create-react-app globally by using the command
npm install -g create-react-app
create-react-app app_name
Step 3: Switch to the project directory
Once the project is created switch to the project directory using the following
command:
cd app_name
Step 4: Start the development server
To run the app, use the following command. This command will locally run your
app. npm start
React Project Structure
The project structure of your React application will look something like this
• src: This folder contain all the necessary source code of the project.
• public: This folder stores the HTML files and the assests.
• node_modules: Dependencies of the project.
• package.json: Used dependencies and scripts information.
• README.md: Any additional information for documentation.
function App() {
const headingStyle = { color: 'green', textAlign: 'center'
};
Hello, World!
</h1>
</div>
);
}
Output:
Hello, World!
Understanding JSX
• JSX stands for JavaScript XML, and it is a special syntax used in
React to simplify building user interfaces.
• JSX allows you to write HTML-like code directly inside JavaScript,
enabling you to create UI components more efficiently.
• Although JSX looks like regular HTML, it’s actually a syntax
extension for JavaScript.
What is JSX?
• Writing JSX: Write JSX just like HTML inside JavaScript files (React
components).
const element = <h1>HeUo, World!</h1>;
return <h1>{message}</h1>;
}
Hello, JSX
works!
• The JSX code <h1>{message}</h1> will be transformed into
JavaScript by Babel. Then react will then create a virtual DOM
element for the <h1> tag with the text inside. and this virtual
DOM is then used to update the actual browser DOM, displaying
"Hello, JSX works!" on the screen.
• After React processes the JSX, it renders the message on the
screen.
Uses of JSX
* Single-page apps
Step 1: Set Up Your Development Environment
Before you start building your React SPA, you need to set up your
development environment.
1. Node.js and npm: Node.js is a JavaScript runtime environment,
and npm (Node Package Manager) is essential for managing your
project dependencies. You can download and install them from
the official Node.js website.
2. Create React App: Facebook’s Create React App is a tool that
sets up a new React project with a standard structure and
configuration. To install it, run the following command in your
terminal:
npx create-react-app my-react-spa 1. Code Editor: An IDE or code
editor like Visual Studio Code is recommended for writing and
managing your code efficiently.
Step 2: Structure Your React Project
Once your React app is created, you’ll see a directory structure
like this:
my-react-spa/
-node_modules/
-public/
-src/
-App.css
-App.js
-App.test.js
-index.css
-index.js
-logo.svg
-.gitignore
-package.json
-README.md
-yarn.lock
header {
background-color: #282c34; padding: 20px; color: white;
}
return (
<div>
<h2>Home Page</h2>
<ul>
{data.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
</div>
);
}
It uses traditional
lifecycle methods
It like
uses useEffect H componentDidMoun
ook for lifecycle t,
methods componentWillUnm
Lifecycle
Methods ount
More boilerplate
Less boilerplate
Code code, especially
code, easier to write
for state and
Complexit and understand
methods
y
Requires method
Simple and direct
binding for event
Event event handling
handling
Handling