Unit 2
Unit 2
js
• The world can't live without mobile and web applications in this day and age.
Everything is digitized, from booking cabs to ordering food to make bank transactions.
Thanks to the efficient frameworks that provide a seamless user experience. One such
robust frontend library is React.
2.1.1 Concepts of React.
Why React?
• Easy creation of dynamic applications: React makes it easier to create dynamic web
applications because it requires less coding and offers more functionality, as opposed
to JavaScript, where coding often gets complex very quickly.
• Improved performance: React uses Virtual DOM, thereby creating web applications
faster. Virtual DOM compares the components’ previous states and updates only the
items in the Real DOM that were changed, instead of updating all of the components
again, as conventional web applications do.
• Reusable components: Components are the building blocks of any React application,
and a single app usually consists of multiple components. These components have
their logic and controls, and they can be reused throughout the application, which in
turn dramatically reduces the application’s development time.
• Unidirectional data flow: React follows a unidirectional data flow. This means that
when designing a React app, developers often nest child components within parent
components. Since the data flows in a single direction, it becomes easier to debug
errors and know where a problem occurs in an application at the moment in question.
• Small learning curve: React is easy to learn, as it mostly combines basic HTML and
JavaScript concepts with some beneficial additions. Still, as is the case with other tools
and frameworks, you have to spend some time to get a proper understanding of
React’s library.
• It can be used for the development of both web and mobile apps: We already know
that React is used for the development of web applications, but that’s not all it can do.
There is a framework called React Native, derived from React itself, that is hugely
popular and is used for creating beautiful mobile applications. So, in reality, React can
be used for making both web and mobile applications.
Unit-2: Fundamentals of React.js
• Dedicated tools for easy debugging: Facebook has released a Chrome extension that
can be used to debug React applications. This makes the process of debugging React
web applications faster and easier.
1. Functional Components
ReactDOM.render(
<React.StrictMode>
<Demo />
</React.StrictMode>,
document.getElementById('root')
);
const Demo=()=>
{
Unit-2: Fundamentals of React.js
2. Class Components
• The class components are a little more complex than the functional components.
• The functional components are not aware of the other components in your program
whereas the class components can work with each other.
• We can pass data from one class component to other class components. We can use
JavaScript ES6 classes to create class-based components in React. Below example
shows a valid class-based component in React.
function Car(props) {
Pass Data
• Props are also how you pass data from one component to another, as parameters.
function Car(props) {
return <h2>I am a { props.brand }!</h2>;
}
function Garage() {
return (
<>
<h1>Who lives in my garage?</h1>
<Car brand="Ford" />
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
Unit-2: Fundamentals of React.js
• When creating a React component, the component's name must start with an upper-
case letter.
• The component has to include the extends React.Component statement, this
statement creates an inheritance to React.Component, and gives your component
access to React.Component's functions.
• The component also requires a render() method, this method returns HTML.
render() {
To use this component in your application, use similar syntax as normal HTML: <Car />
Component Constructor
• If there is a constructor() function in your component, this function will be called when
the component gets initiated.
• The constructor function is where you initiate the component's properties.
• In React, component properties should be kept in an object called state.
• The constructor function is also where you honor the inheritance of the parent
component by including the super() statement, which executes the parent
component's constructor function, and your component has access to all the functions
of the parent component (React.Component).
constructor() {
super();
render() {
Unit-2: Fundamentals of React.js
Components in Components
render() {
render() {
return (
<div>
<Car />
</div>
); } }
root.render(<Garage />);
• if Statement
Example:
function MissedGoal() {
return <h1>MISSED!</h1>;
Unit-2: Fundamentals of React.js
function MadeGoal() {
return <h1>GOAL!</h1>;
}
function Goal(props) {
const isGoal = props.isGoal;
if (isGoal) {
return <MadeGoal/>;
}
return <MissedGoal/>;
}
Operators
Logical && Operator
import React from 'react';
import ReactDOM from 'react-dom/client';
function Garage(props) {
const cars = props.cars;
return (
<>
<h1>Garage</h1>
{cars.length > 0 &&
<h2>
You have {cars.length} cars in your garage.
</h2>
}
</>
);
}
function MissedGoal() {
return <h1>MISSED!</h1>;
}
function MadeGoal() {
return <h1>GOAL!</h1>;
}
function Goal(props) {
const isGoal = props.isGoal;
return (
<>
{ isGoal ? <MadeGoal/> : <MissedGoal/> }
</>
);
}
Lists
function Car(props) {
function Garage() {
return (
<>
<ul>
</ul>
</>
);
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
Example:
function Car(props) {
function Garage() {
const cars = [
];
return (
<>
<ul>
</ul>
</>
);
Unit-2: Fundamentals of React.js
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
• 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
function Football() {
const shoot = () => {
alert("Great Shot!");
}
return (
<button onClick={shoot}>Take the shot!</button>
);
}
Passing Arguments
import React from 'react';
import ReactDOM from 'react-dom/client';
function Football() {
const shoot = (a) => {
alert(a);
}
return (
<button onClick={() => shoot("Goal!")}>Take the shot!</button>
);
}
Unit-2: Fundamentals of React.js
function Football() {
const shoot = (a, b) => {
alert(b.type);
/*
'b' represents the React event that triggered the function.
In this case, the 'click' event
*/
}
return (
<button onClick={(event) => shoot("Goal!", event)}>Take the shot!</button>
);
}