Unit-3 FSD
Unit-3 FSD
REACT JS
React JS
▪ Introduction to React Router
▪ Single Page Applications
▪ React Forms
▪ Flow Architecture
▪ Introduction to Redux
▪ More Redux
▪ Client Server Communication
React JS
▪ ReactJS is a declarative, efficient, and
flexible JavaScript library for building user
interfaces.
▪ It is an open-source, component-based
front-end library that is responsible only
for the view layer of the application.
▪ Use JSX
▪ Virtual DOM
▪ Component
▪ Performance
React JS
How does ReactJS work?
• React creates a virtual DOM in memory to update the
browser’s DOM. The virtual DOM will try to find the most
efficient way to update the browser’s DOM.
React JS
ReactJS Environment Setup
Pre-requisite: 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
Render():-
• The render() method is then called to define the React
component that should be rendered.
But render where?
• There is another folder in the root directory of your
React project, named "public". In this folder, there is
an index.html file.
React in HTML
The Root Node
• The root node is the HTML element where you want to
display the result.
• It is like a container for content managed by React.
• It does not have be a <div> element and does not have to have
the id=‘root’
React using create-react-app
Example:-
/* Hello World*/
Step 1: npx create-react-app HelloWorld
Step 2: cd HelloWorld
root.render(myFirstElement);
Example - 1
function Car() {
return (
<h1>Hi, I'm a Car</h1>
);
}
const root=ReactDOM.createRoot(document.getElementById('root'));
root.render(<Car />);
Example - 1
Example program with create-react-app environment
function Car() {
return (
<h1>Hi, I'm a Car</h1>
);
}
function Vehicle() {
return (
<div>
<h1>What type of vehicle is this!</h1>
<Car/>
</div>
);
}
const root=ReactDOM.createRoot(document.getElementById('root'));
root.render(<Vehicle />);
What is JSX?
❖ JSX stands for JavaScript XML.
❖ You are not required to use JSX, but JSX makes it easier to
write React applications.
❖ Here are two examples. The first uses JSX and the second does
not:
What is JSX?
JSX:
const myElement = <h1>I Love JSX!</h1>;
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(myElement);
Without JSX:
const myElement = React.createElement('h1', {}, 'I do not use JSX!’);
• This approach makes the application more user-friendly and easier to navigate, as
users can see the entire application at once.
• With a traditional web application, users are redirected to a new page every time
they make a change to their data, which can be frustrating if they want to edit
information in multiple places.
Advantages of SPAs
• A single page application has several advantages over traditional web
applications. Here are some of the most notable:
• Simplicity: SPAs are easier to develop and maintain than traditional web
applications. Developers only need to build a single HTML file for the
SPA. No server-side changes are necessary.
• Reusability: You can reuse the same JavaScript, CSS, and HTML code for
multiple pages, and there are tools that allow developers to package these
components into reusable modules.
• Security: SPAs make it easier to secure web pages, because they can be
protected by a firewall or require login credentials.
return (
<form>
<label>Input Value:
<input type="text" value={inputValue} onChange={handleChange}
/>
</label>
<p>Input Value: {inputValue}</p>
</div>
)};
Output
React Form
Example 2:-
import { useState } from "react";
return (
<div>
<label>
Select an option: <select value={selectedOption} onChange={handleDropdownChange}>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
</label>
<p>Selected option: {selectedOption}</p>
</div>
);
}
Output
4. Redux
What is Redux?
• Redux is a state management library that allows you to
manage the state of your JavaScript applications more
efficiently and predictably.
• ACTION: Action is sent or dispatched from the view which are payloads
that can be read by Reducers. It is a pure object created to store the
information of the user's event. It includes information such as type of
action, time of occurrence, location of occurrence, its coordinates, and
which state it aims to change.
• REDUCER: Reducer read the payloads from the actions and then updates
the store via the state accordingly. It is a pure function to return a new state
from the initial state.
Redux
Example:-
At src/app/store.js create the following:
import { configureStore } from '@reduxjs/toolkit’
Then, head to index.js and wrap your App component like so:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<Provider store={store}>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>,
document.getElementById('root')
);
Redux
Counter Component
For this example, we will be creating a simple counter.
When using a Class component, you would see something like this:
import React from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { decrementCounter, incrementCounter } from './counterSlice'
return (
<div>
<div>
<button
aria-label="Increment value"
onClick={() => dispatch(incrementCounter())}
>
Increment
</button>
<span>{count}</span>
<button
aria-label="Decrement value"
onClick={() => dispatch(decrementCounter())}
>
Decrement
</button>
</div>
</div>
)
}
The initial state of the database is stored in the file db.json, which is placed in the root
of the
project:
{
"notes": [
{
"content": "the app state is in redux store",
"important": true,
"id": 1
},
{
"content": "state changes are made with actions",
"important": false,
"id": 2
}
]
}
Client Server Communication
We'll install json-server for the project...
npm install json-server --save-dev
and add the following line to the scripts part of the file package.json
"scripts": {
"server": "json-server -p3001 --watch db.json",
// ...
}
Now let's launch json-server with the command npm run server.
Next, we'll create a method into the file services/notes.js, which uses axios to fetch data from the
backend