Unit 04 - Full Stack Web Development
Unit 04 - Full Stack Web Development
Development
by Dr. Piyush Bagla
React
History
• React was first designed by Jorden Walke, a software engineer at Facebook.
• It was first deployed for the Facebook News feed around 2011.
• In 2013, React was open-sourced at the JS conference.
Key features of React
In React:
State = Data that changes over time inside a component (example: user's input, counter values, fetched API data).
Rendering = How a component draws (or re-draws) itself on the screen based on its state or props.
When either state or props change, React triggers a re-render of the component to reflect the new data in the UI.
In real world:
• State = what’s in your cart (can change).
• Props = sending one product's info to show it nicely.
Component-
Based
Architecture
Declarative Approach
• Defining the UI: In React, you define the desired user interface using components and
JSX. You specify what the components should look like and how they should behave
based on the data (props and state) you provide.
• React's Job: React takes care of rendering the components and updating the actual
DOM based on the defined state and props. It efficiently handles changes to the UI by
using the Virtual DOM and a diffing algorithm.
• Final Result: Just like the pizza, you get a user interface that matches your
description, and React manages the rendering process for you.
Declarative Approach
• You describe what you want, not how they should bake it, mix the dough, add the
toppings, or manage the oven.
You specify what the component should render — not how it gets rendered.
Declarative Approach
Similarly, in React:
• You see the final user interface, which exactly matches your components and
state/props.
• React handles all the heavy lifting invisibly.
DOM
• The Document Object Model is
a model that defines HTML and
XML documents as a tree
structure, where each node of the
tree is an object which represents
a part of the document.
• The DOM stands for Document
Object Model, it is an interface
that allows developers to interact
and control the Elements of the
web.
• Disadvantages of real DOM :
Every time the DOM gets updated, the
updated element and its children have
to be rendered again to update the UI
of our page. For this, each time there
is a component update, the DOM
needs to be updated, and the UI
components have to be re-rendered.
Virtual DOM
using Diffin
using Diffin Algo
Algo
https://blog.logrocket.com/virtual-dom-react/
Virtual DOM
React uses something called batch updates to update the real DOM. It just means that the changes to the
real DOM are sent in batches instead of sending any update for a single change in the state of a component.
This entire process of transforming changes to the real DOM is called Reconciliation.
Diffin Algo
The "Differential Algorithm" in the context of the Virtual DOM refers to the
process by which React identifies and calculates the differences (or "diffs")
between two Virtual DOM trees: the previous Virtual DOM tree and the newly
generated Virtual DOM tree after a component update. This algorithm is at
the core of React's efficient updating mechanism.
Rendering | Render
In the context of React, "render" refers to the process of creating and
updating the user interface (UI) of an application by translating React
components into DOM elements. This involves taking the JSX code or React
components and transforming them into HTML elements that can be
displayed in the browser.
React
For local development, debugging and testing. For live deployment and end-user access.
• These are major modes of React; however, there are other modes as well.
• As of May 2025, the latest stable version of React is React 19.0.0, released on December 5, 2024
Development Mode
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src=https://unpkg.com/react-dom@18/umd/react-dom.development.js crossorigin></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="mydiv"></div>
<script type="text/babel">
const container = document.getElementById('mydiv’);
const root = ReactDOM.createRoot(container);
root.render(<h1>Hello World!</h1>);
</script>
</body>
</html>
Here's a detailed explanation of how the provided code works and the purpose of each part:
Script Inclusions:
1. React Library (`react.development.js`):
- This script includes the React development library.
- It provides the necessary functions and classes for writing React code in your application.
- Since the file is the development version, it includes helpful debugging and error messages to aid in
development.
- Root Container:
- The HTML file includes a `<div>` element with the ID `mydiv`.
- This element serves as the root container for the React application.
- When you render a React application, it needs a root container to attach the React components to.
• What is JSX?
• JSX stands for JavaScript XML.
• It allows writing HTML-like code inside JavaScript.
• Why use JSX?
• Makes React code cleaner and more readable.
• Avoids the need for createElement() and appendChild() methods.
• Converts HTML tags to React elements using React.createElement() behind
the scenes.
• Not Mandatory, but Helpful:
• JSX is not required, but it greatly simplifies UI code in React apps.
JSX Key Syntax Rules
• Embed JS expressions in { }:
const name = "Swastik"; const greeting = <h1>Hello, {name}</h1>;
• If-statements are not allowed inside JSX, but ternary operators are:
const isLoggedIn = true; const message = <p>{isLoggedIn ? "Welcome back!" : "Please sign in."}</p>;
<body>
<div id="root"></div>
</body>
// ChildComponent.js
function ChildComponent(props) {
return <h1>Hello, {props.name}! You are {props.age} years old.</h1>;
}
State
1. Definition: State represents the internal data/state of a component. It is managed within
the component and can be modified over time.
2. Mutable: Unlike props, state is mutable and can be updated using the setState() method
provided by React.
3. Usage:
1. Used for managing component-specific data that may change over time.
2. State is typically initialized in the constructor (for class components) or using the
useState() hook (for functional components).
Most of your components should simply take some data from props and render it. However,
sometimes you need to respond to user input, a server request, or the passage of time. For
this, you use state.
import React from "react";
import ReactDOM from 'react-dom';
• Before hooks, functional components were stateless and couldn't use lifecycle methods
or manage state.
• With hooks, functional components can now manage state, perform side effects, and
have lifecycle behaviors similar to class components.
If you write a function component and
When would realize you need to add some state to it,
I use a previously you had to convert it to a class.
Now , you can use a Hook inside the
Hook? existing function component.
Rules for hooks:
}
return (
<>
<h1>{mytext}</h1>
<button onClick={changetext}>Change</button>
</>
);
}
export default MyComponent;
• In HTML, form data is usually handled by the
DOM.
• In React, form data is usually handled by the
components.
• When the data is handled by the components, all
the data is stored in the component state.
React Forms • You can control changes by adding event handlers
in the onChange attribute.
• We can use the useState Hook to keep track of
each input value and provide a "single source of
truth" for the entire application.
• To access the fields in the event handler use
the event.target.name and event.target.value syntax.
React Forms
function MyForm()
{
return (
<form>
<label>Enter your name: <input type="text" /> </label>
</form> )
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyForm />);
Handling Forms
import React, { useState } from "react";
function ReactForm() {
const [username, setusername] = useState("");
function f1(event) {
setusername(event.target.value);
}
return (
<form action="">
<label>Username
<input type="text" value={username} onChange={f1} />
</label>
</form>
);
}
export default ReactForm;
Submitting Forms
import React, { useState } from "react";
function ReactForm() {
const [username, setusername] = useState("");
function f1(event) {
setusername(event.target.value);
}
function mysubmit(event) {
event.preventDefault();
console.log(username);
}
return (
<form onSubmit={mysubmit}>
<label>Username
<input type="text" value={username} onChange={f1} />
</label>
<input type="submit" />
</form>
);
}
export default ReactForm;
React Router
• React Router enables "client side routing".
• Client side routing allows your app to update the URL from a link click without making
another request for another document from the server. Instead, your app can
immediately render some new UI and make data requests with fetch to update the
page with new information.
• This enables faster user experiences because the browser doesn't need to request an
entirely new document or re-evaluate CSS and JavaScript assets for the next page. It
also enables more dynamic user experiences with things like animation.
React Router
• Components: All components are included in the
initial JavaScript bundle loaded when the app first
starts. React Router handles displaying the correct
component based on the URL.
function App()
{
return (
<BrowserRouter>
<Routes>
<Route path='/' element={<Home/>}/>
<Route path="about" element={<AboutUs/>}/>
Install router
<Route path="contact" element={<ContactUs/>}/>
</Routes> install react-router-dom@6.
</BrowserRouter>
)
} Demonstration for router
export default App; https://reactrouter.com/en/main/start/overview
Router Code – Home.js
import React from "react";
import { Link } from "react-router-dom";
function Home()
{
return(
<>
<header>
<nav>
<ul>
<li><Link to= "/"> Home </ Link></li>
<li><Link to= "/about"> About </ Link></li>
<li><Link to="/contact"> Contact Us</ Link></li>
</ul>
</nav>
</header>
< section>
<h1>Home Page</ h1>
</ section>
</>
)
}
export default Home;
Router Code – AboutUs.js
import React from "react";
import { Link } from "react-router-dom";
function AboutUs()
{
return(
<>
<header>
<nav>
<ul>
<li><Link to= "/"> Home </ Link></li>
<li><Link to= "/about"> About </ Link></li>
<li><Link to="/contact"> Contact Us</ Link></li>
</ul>
</nav>
</header>
< section>
<h1> AboutUs Page</ h1>
</ section>
</>
)
}
export default AboutUs;
Router Code – ContactUs.js
Axios is a popular JavaScript library used to make HTTP requests from the browser or Node.js. It is
often used in React and other JavaScript applications to fetch data from APIs. Axios provides a simple
and clean API for performing various types of HTTP requests such as GET, POST, PUT, DELETE, and
more.