FSD Unit 5
FSD Unit 5
React is a JavaScript library made by Facebook to solve real problems developers face when building
dynamic websites and single-page applications (SPAs).
Traditional websites reload the whole page whenever something changes — this feels slow and old-
fashioned. React solves this by letting developers build apps that update parts of the page instantly
without a full reload.
React uses a component-based approach, which means the UI is split into small, reusable blocks called
components.
Each component has its own logic and layout — this makes it easy to build, test, and reuse pieces across
big projects.
React is also declarative — instead of writing instructions step by step, you write what the UI should
look like, and React takes care of changing it.
This makes code easier to read, debug, and maintain.
React works well for single-page applications that need fast updates — like Facebook, Instagram, and
Netflix.
It also has a huge community and many tools, so it’s easy to find help, learn, and build professional apps.
React’s ideas work across platforms too. You can use React Native to build mobile apps for Android and
iOS using the same React concepts — saving time and effort.
function Welcome() {
This shows how small and reusable React components are — you can use <Welcome /> anywhere in
your app to display the same message.
✅ Conclusion
React is needed because it helps developers build fast, dynamic, and maintainable web applications.
It saves time, keeps code clean, and makes complex UIs easier to handle with components, Virtual DOM,
and declarative syntax — all backed by a big community.
Every React application has a basic structure that helps developers organize their code.
React uses a component-based design — the whole UI is broken into small parts (components).
1⃣ index.html
This is the main HTML page with a <div> with an id like root.
React renders your entire app inside this div.
2⃣ index.js
This is the main JS file. It imports React, the App component, and tells React to render App inside the
root div.
3⃣ App.js
This is the root component. You write your UI here using JSX.
You can break the UI into smaller child components.
my-app/
├── public/
│ └── index.html
├── src/
│ ├── index.js
│ ├── App.js
│ ├── components/
│ │ └── Hello.js
index.js
App.js
function App() {
✅ How It Runs
✅ Conclusion
A simple React structure always has:
All these are separate components that together make up one big parent component, usually called App.
React introduced the component-based model to make big apps easy to build and maintain.
✔ Reusability:
Once you build a component, you can use it anywhere.
For example, a Button component can be used on the home page, login page, or contact page.
✔ Separation of Concerns:
Each component handles its own HTML, CSS, and JavaScript logic.
This makes it easy to test and debug
✔ Faster Development:
Multiple developers can work on different components at the same time without conflict.
✔ Easy to Update:
If a feature changes, you only update that one component — no need to touch the whole project.
✅ Types of Components
1⃣ Functional Components:
These are the most common today.
They are simple JavaScript functions that return JSX.
2⃣ Class Components:
These are ES6 classes that extend React.Component.
They can hold state and lifecycle methods.
Since React 16.8, functional components with Hooks can also handle state, so most developers prefer
functional ones now.
function Welcome(props) {
Here, Welcome is a functional component that takes a name and displays it.
render() {
return <h2>Hello, {this.props.name}!</h2>;
✅ Using Components
Example: A Profile component might get name, age, email as props and show them.
Props are read-only — you cannot change them inside the component
✅ Combining Components
Example:
function App() {
return (
<div>
<Header />
<Content />
<Footer />
</div>
);
✅ Key Benefits
✅ Conclusion
In React, the Virtual DOM is one of the most important reasons why React is fast and efficient
compared to traditional web development.
DOM stands for Document Object Model — it is a programming interface that represents the
structure of a web page.
When you open a website, the browser turns the HTML into the DOM so it can render the page
on the screen.
In a normal web app, every time something changes — like typing in a form or clicking a button
— the real DOM has to update.
But updating the real DOM can be slow, especially if there are many changes at once.
This is where React’s Virtual DOM makes a big difference.
✔ Better performance: Fewer changes to the real DOM means faster updates.
✔ Smooth user experience: Changes appear instantly without flickering or delays.
✔ Cleaner code: Developers don’t have to manually figure out which part of the DOM to update —
React handles that automatically.
✔ Easier maintenance: You focus on what the UI should look like, not how to update it step by step.
This process is called Reconciliation — it ensures your UI stays in sync with your data in the fastest
possible way.
function Counter() {
return (
<div>
<h2>Count: {count}</h2>
);
Here:
When you click Add, React changes the count in the state.
It updates only the <h2> in the real DOM — not the whole page!
✅ Key Points
✅ Conclusion
The Virtual DOM is what makes React different from many other libraries.
By using this smart in-memory copy, React can quickly find what changed and update only what’s
needed — giving users a smooth, high-performance experience.
✔ Reusability: Once you make a component, you can reuse it anywhere in your project.
✔ Readability: Breaking the UI into parts makes your code easier to read and manage.
✔ Maintainability: If something changes, you update only that part — not the whole app.
✔ Teamwork: Multiple developers can work on different components at the same time.
✅ Functional Components:
A functional component is simply a JavaScript function that returns JSX.
Example:
function Greeting() {
These are the most common today because they are simple and easy to write.
✅ Class Components:
A class component is an ES6 class that extends React.Component and has a render() method.
Example:
Before React Hooks were introduced, only class components could handle state and lifecycle methods.
Today, functional components can do everything class components can do — so most developers prefer
the functional style.
The root component organizes the entire UI by combining these child components.
Example:
function App() {
return (
<div>
<Header />
<Content />
<Footer />
</div>
);
Here, <Header />, <Content />, and <Footer /> are all separate components that the root App puts
together.
function Greeting(props) {
Here, the same Greeting component displays different names based on the props.
function Welcome() {
When you use <Welcome /> inside your App component, it displays the message.
✅ Best Practices
✅ Key Points
✅ Conclusion
In React, creating components means building small, reusable blocks of UI that together make up your
whole application.
React does not work like old HTML pages where you write all your code in a single file.
Instead, you break the UI into components and combine them to build pages.
Each piece is its own component, and React combines them all.
Example:
function Welcome() {
return <h2>Hello, React!</h2>;
Example:
render() {
✅ Combining Components
Once you create a component, you can import and use it like a custom HTML tag.
Example in App.js:
function App() {
return (
<div>
<Welcome />
</div>
);
✅ Passing Props
Example:
function Welcome(props) {
In App.js:
✅ Organizing Components
Big React apps have many files and folders to keep things organized:
/src
├── App.js
├── components/
│ ├── Header.js
│ ├── Footer.js
│ ├── ProductCard.js
Greeting.js
function Greeting() {
App.js
function App() {
return (
<div>
<Greeting />
</div>
);
Export it.
Import it in App.
✅ Best Practices
✅ Conclusion
This means data flows in one direction only — from parent components to child components.
In React:
This makes your app predictable and easy to debug, because you always know where the data comes
from.
Example:
A parent App component holds a user name.
It passes the name as a prop to a Greeting child component.
Props are the main way to send data from parent to child.
Example:
function App() {
function Greeting(props) {
Here:
While props are used for passing data, state is used for storing and changing data inside a component.
For example:
A counter value.
Functional components use the useState Hook to create and update state.
Example:
return (
<div>
<h3>Count: {count}</h3>
</div>
);
Here:
count is state.
When the button is clicked, count increases and React re-renders the UI.
Use props to pass that data down to child components for display.
Example:
function App() {
Sometimes, a child needs to send data back — for example, a form input.
Example:
function App() {
function updateName(newName) {
setName(newName);
function InputComponent(props) {
return (
<input
type="text"
/>
);
✅ Key Points
✅ Conclusion
React’s simple, one-way data flow makes apps easy to understand, debug, and grow.
Combining state and props the right way lets you build dynamic apps that are organized, fast, and
maintainable.
Every time your app’s state or props change, React automatically re-renders the affected components.
This means the UI always stays up to date with your data — without you manually updating the DOM
yourself.
Example: If you have a counter and you increase its value, React updates only the part of the page that
shows the number, not the whole page.
This is possible because of React’s Virtual DOM, which finds what changed and updates only that part.
React renders this JSX inside a <div> in index.html (like <div id="root">).
If your data changes — for example, a user types in a text box — React re-renders only the parts that
changed.
✅ Why Is Rendering Important?
Life Cycle Methods are special functions you can use in class components to run code at different points
of a component’s life.
Life cycle methods let you run custom code when these things happen.
1⃣ constructor()
2⃣ render()
Required.
3⃣ componentDidMount()
4⃣ componentDidUpdate()
5⃣ componentWillUnmount()
Good for cleaning up — for example, clearing timers or removing event listeners
Modern React uses Hooks to handle life cycle behavior in functional components.
Example:
function Timer() {
useEffect(() => {
return () => {
console.log('Component Unmounted');
};
return (
<div>
<h3>Count: {count}</h3>
</div>
);
✅ Key Points
✔ Rendering is how React shows your UI on screen.
✔ React’s Virtual DOM makes rendering fast and efficient.
✔ Life cycle methods let you run extra code during mounting, updating, and unmounting.
✔ Today, functional components use Hooks (useEffect) for life cycle logic.
✅ Conclusion
Understanding rendering and life cycle methods helps you build React apps that are dynamic, fast, and
able to handle real-world tasks like fetching data, updating the page, and cleaning up resources when a
component is removed.
In plain HTML, you use <form>, <input>, <textarea>, and <button> elements.
In React, you still use these same elements, but you handle the data differently using state.
The form elements get their current values from React state.
So, the React component controls the form, not the browser’s default behavior.
✔ Keeps form data in React state, so you always know what the current values are.
✔ Makes it easier to validate user input.
✔ Makes it easy to handle submit actions.
✔ Keeps all your app’s data flow in one place, instead of mixing with the DOM.
function handleChange(e) {
setName(e.target.value);
function handleSubmit(e) {
e.preventDefault();
return (
<form onSubmit={handleSubmit}>
<label>
Name:
</label>
<button type="submit">Submit</button>
</form>
);
Example:
function handleChange(e) {
In React, <textarea> and <select> are also controlled the same way.
Example:
</select>
Since form values are stored in state, you can easily check the values when the form is submitted.
Example:
function handleSubmit(e) {
e.preventDefault();
alert('Name is required!');
return;
}
// Continue...
✅ Uncontrolled Components
While React recommends controlled components, you can also make uncontrolled components, which
use the DOM to store values instead of React state.
Example:
function handleSubmit(e) {
e.preventDefault();
alert(inputRef.current.value);
✅ But this is less common — controlled components are preferred because they follow React’s one-
way data flow.
In regular HTML:
In React:
You have full control to check, change, and submit that data exactly how you want.
✅ Best Practices
✅ Key Points
React forms use controlled components.
The user’s input updates the state, and the state updates the input’s value.
✅ Conclusion
In web development, third party libraries are pre-written pieces of code made by other developers or
companies.
They help you add features faster, without having to build everything from scratch.
React is a library for building user interfaces — but it does not include everything by default.
Sometimes you need extra features like:
Form validation
HTTP requests
Animations
For these tasks, developers use third party libraries that work well with React.
1⃣ React Router:
Used for adding routing and navigation in single-page apps.
Example: Switching between Home, About, and Contact pages without reloading the entire page.
2⃣ Axios or Fetch:
Used for making HTTP requests (API calls).
Example: Getting user data from a server.
4⃣ Lodash:
A utility library with helpful functions for working with arrays, objects, and strings.
5⃣ Chart.js or Recharts:
Used for adding charts, graphs, or data visualizations.
Most libraries are installed using npm (Node Package Manager) or yarn.
Example:
or
This adds the library to your node_modules folder so you can import and use it in your React app.
Example:
function GetData() {
function UserList() {
useEffect(() => {
axios.get('https://jsonplaceholder.typicode.com/users')
}, []);
return (
<div>
<h2>User List</h2>
<ul>
</ul>
</div>
);
✅ How it works:
✔ Always read the documentation for each library to learn how to use it properly.
✔ Install only trusted, well-maintained libraries.
✔ Keep your dependencies updated to avoid security issues.
✔ Only add libraries when you really need them — too many can slow down your app.
Example:
✅ Key Points
Keep your app clean and lightweight — don’t add libraries you don’t really need.
✅ Conclusion
Integrating third party libraries in React makes your app more powerful, saves time, and gives you
access to tools built by experts.
It’s an important skill for every React developer — because real-world projects often rely on multiple
libraries to build modern, user-friendly apps.
� Topic 11: Routing in React
When you build a single-page application (SPA) with React, all your content loads on one HTML page.
But users still expect to navigate between different pages like Home, About, Contact, etc. — without
reloading the whole page.
Routing is how you let users move between pages smoothly, while React loads and displays different
components based on the URL.
React itself does not include routing by default — instead, developers use a popular third-party library
called React Router.
React Router is the standard library for adding routing to React apps.
It lets you:
✔ BrowserRouter
Wraps your entire app and keeps track of the URL.
✔ Routes
A container for all your Route components.
✔ Route
Defines a path (URL) and tells React which component to show.
✔ Link
Used to create links that change the URL without reloading the page.
Here’s a simple example of a React app with three pages: Home, About, and Contact.
1⃣ App.js
function App() {
return (
<BrowserRouter>
<nav>
<Link to="/contact">Contact</Link>
</nav
<Routes>
</Routes>
</BrowserRouter>
);
2⃣ Example Home.js
function Home() {
You do the same for About.js and Contact.js — each returns its own content.
✅ Nested Routes
React Router also supports nested routes, so you can build pages inside pages — like a Dashboard with
sub-pages for Profile, Settings, etc.
✅ Real-World Example
Apps like Facebook, Instagram, and Gmail are all single-page apps that use routing to switch between
pages without full reloads.
✅ Important Tips
✅ Key Points
Routing makes React apps feel like real websites with multiple pages.
You can use nested routes and dynamic routes for bigger apps.
✅ Conclusion
Routing in React helps you build apps with multiple pages, clean URLs, and smooth navigation — all
inside a single-page application.
This makes your app look and feel professional, keeps users happy, and is a must-know skill for every
React developer.