REACT.JS PPT-2
REACT.JS PPT-2
e to
React js
Introduction to React js
Q. What is React.js?
React.js is a Frontend Javascript library used for building
user interface (UI’s).
Q. What is Framework?
Framework is a complete structure for building an application. It provides rules,
patterns, and tools, and often dictates how your code should be structured. It is built
over your libraries and there is a proper syntax and rules we have to follow. You can't
do and use it however you see fit but it provides you with everything, no need of
anything else.
Uses client-side routing (React Router) Uses server-side routing (URLs trigger
to switch views without reloading. full-page reloads).
The SPA is not a new term it present since 2002. But we didn’t
have technology at that time.
• Component-Based Architecture :
A component in React splits your UI into small, reusable pieces. Each component
handles its own logic and rendering, making your app modular and
maintainable.
• Declarative :
Declarative means you tell React what you want, and React figures out how to do it. Instead
of manually updating the DOM, you describe the UI state, and React updates it efficiently.
Unidirectional Data Flow in React means data flows in one direction - from parent
component to child components via props. This ensures predictable data flow, making it
easier to debug.
"Learn Once, Write Anywhere" in React Native means you can use your React knowledge
to build apps for multiple platforms (web, iOS, Android) without learning separate
technologies for each.
• Virtual DOM:
React uses a Virtual DOM to optimize updates. React does not update the real DOM directly
because it's slow. Instead, it uses a Virtual DOM, which is a lightweight copy of the real
DOM.
Without React.js vs With React.js
Q. Without react.js? Q. With react.js?
In traditional JavaScript, every UI React does not update the Real DOM
update directly modifies the Real directly.
DOM. The problem is that the DOM is
slow. Instead of it uses virtual dom.
And DOM manipulation was very Keeps UI fast and smooth, even in
expensive. large applications.
How React Internally works
DOM is the representation of UI in the form of tree data structure used by browser.
Any update with the dom result in the re-rendering or re-painting of the UI.
React is not using real dom instead uses Virtual dom, which is a temporary data copy
of real dom.
This virtual dom is the copy(clone) of real dom and no re-rendering takes place.
It will patch only particular node and update it. It will compare each node with node of
real dom and update only the changed node and don't update other node and avoid
re-rendering.
React does not interact with real dom directly, but instead it interacts with virtual
dom which interacts with real dom.
How React Internally works
Reconciliation :
Reconciliation in React is the process through which React updates the actual DOM
to match the latest version of the virtual DOM. It's a core mechanism that ensures
efficient UI rendering and updates.
Updating the Real DOM - Once the diffing algorithm identifies the changes,
React generates a set of "patches" — instructions for updating the real DOM.
These patches are applied in a batched manner, minimizing the number of DOM
manipulations and improving performance. This ensures that the browser only
performs the minimum required updates, resulting in improved performance.
React js Applications
Prerequisites for Learning React.js
HTML, CSS, JAVASCRIPT:
i. HTML (Hyper Text Markup Language) – The structure of a webpage (e.g.,
headings, buttons, forms).
ii. CSS (Cascading Style Sheets) – Used for styling the webpage (e.g., colors,
layouts).
iii. JavaScript (ES6+) – Arrow functions, let and const variables, template literals,
destructuring (arrays & objects), spread and rest, modules (import/export),
events, and event handling. DOM manipulation
Installing React.js with Vite
Vite is frontend build tool and development server.
Vite is a faster and lightweight.
It provides better performance and a smoother developer experience.
`
Step-1: Install Node.js
Before installing React, make sure Node.js is installed on your system.
After installation open command prompt (cmd) to check the version of installed
node .js
Command: node -v
`
Step-3: Open this folder inside the vs
code
Click Terminal – New Terminal
Click (+) icon and open the command prompt not Powershell.
`
Step-4: Run the following command in
cmd
Command: npm create vite@latest
And just enter on keyboard.
`
Step-5: Packages and Project Name
It will ask to install some packages, you have to give the permission by typing:
Ok to proceed: y
Then after hitting enter it will ask for project name.
Give the project name whatever you want.
For Example - Project name: my-first-create-app
`
Step-5: Select a framework
After giving name it will ask the select a framework.
Only Select – React
How to select ? – Use down arrow key on keyboard for selecting option react
option.
For Example – Select a framework: React
`
Step-5: Select a variant
After selecting a framework it will ask for you variant.
Variant is nothing but which language you are going to use in react js
How to select ? – Use down arrow key on keyboard for selecting option javascript
option.
For Example – Select a variant: Javascript (✅)
Don’t select Javascript+swc (❌) `
Step-6: 3 commands will be created
commands in the
terminal.
Now use step-7 on
next slide
Step-7: Use first command
`
Step-8: Use second command
Now you are entered in the react project folder.
Copy the second command and paste into the cmd.
And hit enter, it will install important packages which will help to run the react
project.
Command : npm install (It will create one node-modules folder)
It will take some tine to install the packages.
` so wait for some time. (It might takes
After Installation
some minutes also, it depends upon your internet speed)
Step-9: Use third and last command
Now you can right side image, your folder structure will look like this.
That means you have created your first react app.
To run react project you need last command copy the command and hit enter.
Command: npm run dev
`
Step-10: Copy the link and Paste into
browser
After the step-9 it will create one link.
Just copy the link and paste it into the browser.
Now you can see react project is running on the browser successfully.
`
Copy this link paste in the
browser
Congratulations you have created first
react project
`
Three main libraries of React
• React :
The core library for building UI components. Provides functions like useState,
useEffect, and many more. Works with the Virtual DOM to optimize updates.
• ReactDOM :
Connects React to the browser’s real DOM. Handles rendering React components into
the actual webpage.
• Babel :
A JavaScript compiler that converts modern JS (JSX, ES6+) into plain JavaScript.
Helps browsers understand JSX. Used in development to make React code browser-
compatible.
React Folder Structure
Browser internally understands react elements, as virtual dom uses react element.
To create elements in react we use a method createElement( ), provided by react
library.
This createElement is a difficult syntax to follow, and we are already familiar with
html, that why we use jsx in react for template purpose.
JSX (Javascript and XML)
1. JSX elements must be closed (opening tag and closing tag are mandatory with
proper syntax). Even if it is an empty/void tag that also needs to be closed.
2. JSX elements must have one parent element. Elements at same level should be
wrapped in one parent element.
3. To provide a class to react element, we have to use className as class is a
keyword in javascript. `
4. To link your label with input tag in form, we have to go with htmlFor as for is a
reserve keyword in javascript.
5. JSX follows naming convention – inbuilt tags should be in lowercase,
attributes should be in camelCase and custom build components should be
in PascalCase.
What is Fragment in React.js?
It accepts only one attribute that is key, we will see this key later.
1. Using <React.Fragment>
`
Components in React js
`
How to create component?
Create a new component file in the src folder - You should use the extension as .jsx
only.
component.
And import it with proper address wherever you feel necessary and use it as
Function Based Component in React js
A functional component is just a Javascript function that returns JSX.
`
Extension – React Developer Tools
React Developer Tools is a browser extension that helps inspect and debug React
components in real-time.
It allows developers to view component hierarchy, props, state, and performance,
making debugging easier.
`
List and Keys in React js
In React, a list is a collection of items (like an array) that we render dynamically using
.map()
`
added, or removed.
`
How to pass different javascript values as a
prop
If you are passing string you can just pass it normally in quotes, we can not use
backticks.
If you want to send any data type other than string, we should go with expression
( { } ).
We need to wrap data inside jsx expression ( { } ).
We can also pass string in jsx expression. `
Props Children
Till now we are using unpaired syntax for our custom tags, but what if we use the
React automatically will convert that content into props and it will be present
We can pass JSX only as content in props.children (between opening and closing
custom tags).
The passed JSX is present in the form of array in the children key.
Props Children
`
Events in React
Events in React are user interactions such as clicks, key presses, or mouse
movements.
React handles events using event listeners, similar to JavaScript, but with a slightly
different syntax.
`
Important Points:
3. The event object (e) provides the information about the event.
Events Handling in React
In react, event handling is similar to handing events in javascript but with some
key differences.
consistency.
`
Events Handling in React
`
e.preventDefault() in React
event.
This is commonly used in submissions forms, links clicks, and other default
browser behaviors.
`
Syntax – e.preventDeafult();
on a condition.
render.
`
1. Conditional Rendering By Using if-else
`
2. Conditional Rendering By Using Ternary
Operator
The ternary operator is used as a shorter syntax for the if-else statement.
`
3. Conditional Rendering By Using Short-
Circuit
Short Circuit (&&) (AND Operator).
The short-circuit operator works from left to right. It is alternate for the if
statement. If the condition is true jsx will be rendered.
It works inside the JSX ({ }).
`
States in React
based component.
`
Global CSS
In the global css file, we can write the normal css code using selectors.
Inline styles in React are written as Javascript Objects inside the style attribute.
First close your project (terminate the project in terminal by clicking ctrl+c – then
type y and hit enter)
Go to this website - https://tailwindcss.com/docs/installation/using-vite
You will see this type of UI.
`
Step-1: Copy the first command
Copy the first command and open new terminal - Command prompt – and paste
the command and hit enter.
`
Step-2: Copy the 2nd import statement
Copy the 2nd import statement only - import tailwindcss from '@tailwindcss/vite‘
Then go to vs code you will see file vite.config.js at the last of the project.
Open the file paste that import statement in the line no-3.
`
Step-3: Copy the tailwindcss()
Before After
`
Step-4: Copy the 3rd command and paste in
global.css
Now create one file inside the src folder. – filename - global.css
Inside that paste the 3rd command - @import "tailwindcss";
`
Step-5: Now import that global.css in
main.jsx
Now import that global.css file import inside the main.jsx file.
Or you can type exact same import statement inside your code in line-4
`
Step-6: Finally run the react project – npm
run dev
Tailwind installation done for your system.
Now just run the react project in terminal – command prompt – npm run dev
Open the link in the browser that it done.
`
For those who are getting errors
`
For those who are getting errors
Next thing just scroll the page you will see this type of one box.
In the terminal, just check what it the version error coming and it may be asking
like x64 or x86 according that just click on the link and download that package in
your system.
`
For those who are getting errors
After downloading just check the I agree and click on the install button.
After the completion of installation just restart your laptop.
Open the vs code and run the react project now that error will not come.
`
Forms Handling React
Form handling in react means managing the input fields (like text, email,
password, radio, select, etc) and their values when a user types something.
It includes:
`
Forms Handling React
1. State :
The state holds the form's input values.
It updates whenever the user types something in the input fields.
2. onChange event :
The onChange event updates the state when the user types something in the
`
input field.
3. value attribute :
The value attribute binds the input field to the state.
4. name attribute :
The name attribute helps identify the input field and allows us to update the
correct state property.
useEffect()
render.
`
2. Unwanted side effects
useEffect() Hook
`
Why Do We Need Context API?
`
Steps to Create Context API
`
nsume Data: const data = useContext(MyContext);