UNIT 2
UNIT 2
UNIT 2
JS
2.1 Overview of React
2.1.1 Concept of React
2.1.2 Using React with HTML
2.1.3 React interactive components : Components within
Components and files
2.1.4 Passing Data through props
2.2 Class Components
2.2.1 React class and Class Components
2.2.2 Conditional Statements, Operators, Lists
2.2.3 React Events: Adding Events, Passing arguments, Event
Objects
Introduction
ReactJS is a declarative, efficient, and flexible
JavaScript library for building reusable UI components.
It is an open-source, component-based front end
library responsible only for the view layer of the
application.
It was created by Jordan Walke, who was a software
engineer at Facebook.
It was initially developed and maintained by Facebook
and was later used in its products like WhatsApp &
Instagram.
Facebook developed ReactJS in 2011 in its newsfeed
section, but it was released to the public in the month of
May 2013.
Today, most of the websites are built
using MVC (Model View Controller)
architecture. In MVC architecture,
React is the 'V' which stands for View,
whereas the architecture is provided
by the Redux or Flux.
Latest React Version is 18.0 which was
introduced on 29th March, 2022
To learn REACTJS, basic requirements
are HTML, CSS, JS and ECMAScript
(European Computer Manufacturers
Association Script)
2.1 Overview of React
2.1.1 Concept of React
2.1.2 Using React with HTML
2.1.3 React interactive components :
Components withing Components and
files
2.1.4 Passing Data through props
1. Install node.js
nodejs.org
2. Check installed node.js version
Node -v
3. To install create-react-app
Npx create-react-app app_name
4. To check installed create-react-app version
create-react-app --version
5. Create new folder where you want to create your project
Mkdir folder_name
6. Change directory to new folder
Cd folder_name
7. Inside folder run this command to create react app
Npx create-react-app app_name
8. To run react app
Npm start
Installation of REACT
1. Install NODEJS and NPM
2. Install Visual Code / Sublime / Atom
/ Brackets
3. Install React from terminal
Npm install –g create-react-app
Create-react-app --version
Create-react-app <projectname>
Installation of REACT in
OFFLINE MODE
1. Install node.js
nodejs.org
2. Check installed node.js version
Node -v
3. Change directory from current to project
folder.
4. Run the following command.
npm install -g create-react-app-offline
Crao –n myapp
2.1.1 Concept of React
React is a JavaScript library for
building user interfaces.
React is used to build Single-Page
Applications.
React allows us to create reusable UI
components.
2.1.2 Using React with HTML
ReactDOM.render()
render has been replaced with createRoot in React 18.
React's goal is in many ways to render HTML in a web
page.
React renders HTML to the web page by using a function
called ReactDOM.render().
The ReactDOM.render() function takes two arguments,
HTML code and an HTML element.
The purpose of the function is to display the specified
HTML code inside the specified HTML element.
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.
Example:
index.js
var React=require('react');
var ReactDOM=require('react-dom');
ReactDOM.render(<h1>Hello
World</h1>,
document.getElementById('root')
)
index.html
<body>
<div id="root"></div>
</body>
Exercise
Make a website header and display on
webpage.
Make a table with 5 students detail
and print it on browser.
Use 2-3 images and display on
browser.
Make one webpage that contains h1, p
and 5 ordered list elements (Netflix,
Web Series, name of 5 web series)
React JSX
JSX stands for JavaScript XML.
JSX allows us to write HTML in React.
JSX makes it easier to write and add
HTML in React.
JSX converts HTML tags into react
elements.
Example:
const myElement = <h1>TYBCA The
Great Class</h1>;
const root =
ReactDOM.createRoot(document.getElem
entById('root'));
root.render(myElement);
2.1.3 React interactive components :
Components within Components and files
function Car(props) {
return <h2>I am a {props.color} Car!</h2>;
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Car color="red"/>);
2.2 Class Components
2.2.1 React class and Class
Components
2.2.2 Conditional Statements,
Operators, Lists
2.2.3 React Events: Adding Events,
Passing arguments, Event Objects
2.2.1 React class and Class
Components
2.2.2 Conditional Statements,
Operators, Lists
In React, you can conditionally render
components.
There are several ways to do this.
Without Conditional
function MissedGoal() {
return <h1>MISSED!</h1>;
}
function MadeGoal() {
return <h1>Goal!</h1>;
}
With Component
function Goal(props) {
const isGoal = props.isGoal;
if (isGoal) {
return <MadeGoal/>;
}
return <MissedGoal/>;
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Goal isGoal={false} />);
Operators
Logical && Operator
Ternary Operator
Logical && Operator
import React from 'react';
import ReactDOM from 'react-dom/client';
function Logicaland()
{
const a=10,b=20;
return (
<>
{
a<b &&
<h2>VTCBCSR</h2>
}
</>
);
}
}
</ul>
</>);
}
function Chocolate(props)
{
return <li>I like {props.brand}</li>
}
2.2.3 React Events: Adding Events, Passing
arguments, Event Objects
Just like HTML DOM events, React can perform
actions based on user events.
React has the same events as HTML: click,
change, mouseover etc.
Adding Events
React events are written in
camelCase syntax:
onClick instead of onclick.
React event handlers are written
inside curly braces:
onClick={shoot} instead of
onClick="shoot()".
HTML Events
<button onclick="shoot()">Take the
Shot!</button>
REACT Events
<button onClick={shoot}>Take the
Shot!</button>
Example:
import React from 'react';
import ReactDOM from 'react-dom/client';
function Event()
{
const call=()=>{
alert("Event Done");
}
return (
<button onClick={call}>Click Me</button>
)
}
export default Event;
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Event />);
Passing Arguments
To pass an argument to an event
handler, use an arrow function.
import React from 'react';
import ReactDOM from 'react-dom/client';
function Event()
{
const call=(a)=>{
alert(a);
}
return (
<button onClick={()=>call("Demo")}>Click Me</button>
)
}
export default Event;
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Event />);
Event Objects
Event handlers have access to the
React event that triggered the function.
Example:
function Event()
{
const call=(a,b)=>{
alert(b.type);
}
return (
<button onClick={(event)=>call("Demo",event)}>Click
Me</button>
)
}
export default Event;
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Football />);