Unit - 1 - Mohd Arif
Unit - 1 - Mohd Arif
Unit: 1
Introduction to React JS
Dr. Mohd Arif
Course Details (Assistant Professor)
(B. Tech. 7th Sem)
CSE (Data Science)
Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 1
07/10/2024 Unit 1
Faculty Introduction
Name Dr. Mohd Arif
Qualification Ph.D.
Subject Taught C Programming, Software Engineering, Computer Graphics, Core JAVA Programming
Language, C++, HTML & CSS , MEAN Stack.
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 2
Unit 1
Syllabus
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 3
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 4
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
The MEAN stack is known for its ease of use, flexibility, and scalability.
It's a great choice for building web applications that require real-time
interactions and high-performance capabilities.
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 6
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 7
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
The MERN stack is known for its speed, efficiency, and flexibility. It's a
great choice for building web applications that require real-time
updates and a modern user interface.
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 8
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 9
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 10
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 11
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
Community Support
Both the MEAN stack and the MERN stack have strong and active
communities. However, React has a larger and more diverse
community, which means that there are more resources available for
learning and problem-solving.
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 12
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
Popularity
In terms of popularity, both stacks are widely used and well-regarded
in the web development community. However, React is currently
more popular than AngularJS, which can be attributed to its ease of
use, performance, and the large community support it has gained
over the years.
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 13
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
React JS
React is a JavaScript-based UI development library. Facebook and an
open-source developer community run it. Although React is a library
rather than a language, it is widely used in web development. The
library first appeared in May 2013 and is now one of the most
commonly used frontend libraries for web development.
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.
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 14
Unit 1
WEB DEVELOPMENT USING MERN STACK WITH DEVOPS
Node JS ()
Node allows developers to write JavaScript code that runs directly in
a computer process itself instead of in a browser. Node can,
therefore, be used to write server-side applications with access to the
operating system, file system, and everything else required to build
fully-functional applications.
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 16
Unit 1
NPM commands
NPM stands for Node Package Manager, is one of the most used tools for
any javascript developer. It puts all modules in one place so that node
can find them, and manages dependency conflicts.
Here's a list of the most common npm commands you should be aware
of.
• install
This command is used to install the npm package and the other packages
which the particular package depends on. It will install in the local
node_modules folder.
npm install <packagename>
WEB DEVELOPMENT USING MEAR STACK with
07/10/2024 DEVOPS Unit 1 17
NPM commands
uninstall
This command does the exact opposite of install. It will uninstall the package completely which is already exist in
the node_modules folder. If the package mention is not present package.json list or node_modules folder, it won't
do anything.
This command updates the current package to the latest version and if no package name has been specified then
it will update all packages. If some package is missing, it will check and update those too.
This command will print all the packages and their versions that are installed, as well as their dependencies in a
tree structure.
npm list
view
help
This command helps with the mentioned topic. It shows the appropriate documentation page.
If the topic does not exist, or if multiple terms are provided, then npm will run the help-search command to find a
match.
build
start
This command runs a predefined command specified in the start property available inside scripts in the package.json
file.
npm start
stop
This command runs a predefined command specified in the stop property available inside scripts in the package.json
file. Unlike start, there is no default script that will run if the stop property is not defined
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 21
Unit 1
Syllabus
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 22
Unit 1
Syllabus
07/10/2024 Dr. Mohd Arif WEB DEVELOPMENT USING MEAR STACK with DEVOPS 23
Unit 1
Syllabus
Students examine advanced topics like Reactjs, Devops, Mongodb for web
applications.
Also examine Express framework and Devops for interactive web applications
that use rich user interfaces. .
CO2 : Understand the impact of web designing by database connectivity with Mongodb .
CO3 : Explain, analyze and apply the role of server side scripting language like Nodejs
and Express framework.
CO4 : Identify the benefits of DevOps over other software development processes to
Gain insights into the DevOps environment.
CO5 : Demonstrate popular open-source tools with features and associated terminology
used to perform Continuous Integration and Continuous Delivery.
PO8 : Ethics
PO10 : Communication
CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 2 2 2 3 3 - - - - - - -
CO2 3 2 3 2 3 - - - - - - -
CO3 3 2 3 2 3 - - - - - - -
CO4 3 2 3 2 3 - - - - - - -
CO5 3 2 3 3 3 - - - - - - -
CO1 3 - - -
CO2 3 3 - -
CO3 3 3 - -
CO4 3 3 - -
CO5 3 3 - -
Program Educational
PEOs Description
Objectives (PEOs)
To have an excellent scientific and engineering breadth so as to comprehend,
PEOs analyze, design and provide sustainable solutions for real-life problems using state-
of-the-art technologies.
To have life-long learning for up-skilling and re-skilling for successful professional
PEOs career as engineer, scientist, entrepreneur and bureaucrat for betterment of society.
• https://youtu.be/QFaFIcGhPoM?list=PLC3y8-rFHvwgg3vaYJgHGnModB54rxOk3
• https://youtu.be/pKd0Rpw7O48
• https://youtu.be/TlB_eWDSMt4
• https://youtu.be/QFaFIcGhPoM
• https://youtu.be/EHTWMpD6S0
MERN stack comprises the following technologies that allow for faster
application development:
Learning the MERN stack will help you become a full-stack developer.
The demand for MERN stack developers is also high. However, it takes time to be a good MERN stack
developer.
So, before learning technologies like React or Node.js, it is important to ensure that your fundamentals
are strong.
A lot of developers fail to be good MERN stack developers because they lack the fundamental
knowledge of JavaScript.
Frameworks are a piece of software that offers a way to create and run web applications. Thus, you don’t need to
code on your own and look for probable miscalculations and faults.
As web standards began to advance, the app logic shifted towards the client- ensuring smarter communication
between the user and the web application. With logic on client-side, they (client) can react swiftly to user input.
This makes web apps more responsive, easily navigate-able on any device. Thus, we have two functions of
frameworks —
a) The one to work on the server side, that helps to set up app logic on the server (backend)
b) To work on the client-side (front end).
• NET (C#)
• Django (Python)
• Ruby on Rails (Ruby)
• Express (JavaScript/Node.JS)
• Symfony (PHP)
• Angular
• Express.JS
• Vue.JS
• React.JS
NPM stands for Node Package Manager, is one of the most used tools
for any javascript developer. It puts all modules in one place so that
node can find them, and manages dependency conflicts.
Here's a list of the most common npm commands you should be aware
of.
• install
This command is used to install the npm package and the other
packages which the particular package depends on. It will install in the
local node_modules folder.
npm install <packagename>
SWETA PANDEY WEB DEVELOPMENT USING
07/10/2024 MEAR STACK with DEVOPS Unit 1 51
NPM commands
uninstall
This command does the exact opposite of install. It will uninstall the package completely which is already exist in
the node_modules folder. If the package mention is not present package.json list or node_modules folder, it won't
do anything.
This command updates the current package to the latest version and if no package name has been specified then
it will update all packages. If some package is missing, it will check and update those too.
This command will print all the packages and their versions that are installed, as well as their dependencies in a
tree structure.
npm list
view
help
This command helps with the mentioned topic. It shows the appropriate documentation page.
If the topic does not exist, or if multiple terms are provided, then npm will run the help-search command to find a
match.
build
start
This command runs a predefined command specified in the start property available inside scripts in the package.json
file.
npm start
stop
This command runs a predefined command specified in the stop property available inside scripts in the package.json
file. Unlike start, there is no default script that will run if the stop property is not defined
React app
Refers to a web application built using the React JavaScript library.
React is a popular and widely-used open-source library developed by Facebook for
building user interfaces.
It provides a component-based architecture and utilizes a virtual DOM (Document
Object Model) to efficiently update and render components when the underlying data
changes.
In a React app, the user interface is divided into reusable components, which are
small, self-contained building blocks.
Each component encapsulates its own logic, styles, and rendering.
These components can be combined and nested to create complex user interfaces.
React follows a declarative approach, where developers describe how the UI should
look based on the current state of the application.
When the state changes, React automatically re-renders the affected components,
efficiently
07/10/2024
updating only the necessary
SWETA PANDEY
parts .
MEAR STACK with DEVOPS
WEB DEVELOPMENT USING
Unit 1 55
React App
Key features of a React app include:
Component-Based Architecture: React apps are built by composing and nesting reusable
components, making the codebase modular and maintainable.
Virtual DOM: React utilizes a virtual representation of the DOM, which allows it to efficiently update
and render components by minimizing direct manipulations to the actual DOM.
JSX: JSX is a syntax extension for JavaScript that enables developers to write HTML-like code within
their JavaScript files, making it easier to describe the structure and appearance of the UI.
State Management: React provides a mechanism to manage and update application state using the
useState hook or more advanced state management libraries like Redux or MobX.
Lifecycle Methods: React components have lifecycle methods that allow developers to perform
certain actions at specific stages of a component's life, such as when it is mounted, updated, or
unmounted.
SWETA PANDEY WEB DEVELOPMENT USING
07/10/2024 MEAR STACK with DEVOPS Unit 1 56
React App
Key features of a React app include:
React Router: React Router is a popular library for handling routing in React apps,
enabling navigation between different views or pages without reloading the entire page.
Ecosystem and Community: React has a vast ecosystem with a wide range of libraries,
tools, and community support, making it easier to find solutions, extensions, and
resources for building complex applications.
Create a React app, here are the general steps you can follow:
Create a New React App: Open your terminal or command prompt and
navigate to the directory where you want to create your React app. Run
the following command to create a new React app using Create React
App:
npx create-react-app my-app
Replace "my-app" with the desired name of your app.
SWETA PANDEY WEB DEVELOPMENT USING
07/10/2024 MEAR STACK with DEVOPS Unit 1 58
React App
Navigate to the App Directory: Once the app is created, navigate into the app
directory using the following command:
cd my-app
Again, replace "my-app" with the actual name of your app.
Start the Development Server: Start the development server to see your React app
in action. Run the following command:
npm start
This will start the development server and open your React app in your default web
browser.
Customize Your App: Open the code of your React app in a text editor or an
integrated development environment (IDE) of your choice. The main file you'll be
working with is src/App.js. Modify this file to customize your React app by adding
components, styles, and logic.
SWETA PANDEY WEB DEVELOPMENT USING
07/10/2024 MEAR STACK with DEVOPS Unit 1 59
React App
Add Additional Components: Create additional components in separate files within
the src directory to encapsulate different parts of your app's functionality. You can
import and use these components within src/App.js or any other component file.
Install Dependencies: If you need to use external libraries or packages in your React
app, you can install them using npm. For example, if you want to install a UI library
like Material-UI, you can run the following command:
npm install @mui/material
These steps provide a basic overview of creating a React app. There are many more concepts and
features you can explore while building your React app, such as routing, state management, and
integrating with backend APIs. React documentation and online tutorials can be helpful resources for
diving deeper into these topics.
React Component:
Components are the building blocks of a React application and are responsible for
rendering elements on the screen.
Functional Components:
Functional components are JavaScript functions that return JSX (JavaScript XML) to define the structure and appearance of the
component.
They are typically used for simpler components that don't require complex state management or lifecycle methods.
Example of a functional component in React:
import React from 'react';
Class Components:
Class components are JavaScript classes that extend the React.Component class. They
provide more features and flexibility compared to functional components.
Class components have their own state, lifecycle methods, and more advanced
functionality.
Class Components:
Class components are JavaScript classes that extend the React.Component class. They
provide more features and flexibility compared to functional components.
Class components have their own state, lifecycle methods, and more advanced
functionality.
Both functional and class components can be used interchangeably in a React application, but
functional components are preferred for their simplicity and performance advantages. However, class
components are still useful for more complex scenarios that require state management or lifecycle
methods.
Components can be composed and nested within each other to build a hierarchy of UI elements,
creating a tree-like structure. This allows for reusable, modular, and maintainable code.
Topic : Basic Understanding JSX, Props and State, Stateless and Stateful
Components, Component life cycle, Hooks, react-router vs react-router-
dom.
In this topic, the students will learn about the JSX, Props and State,
Stateless and Stateful Components. Student will learn regarding
Component life cycle, Hooks, react-router vs react-router-dom.
.
JavaScript Expressions: JSX allows you to embed JavaScript expressions within curly
braces {}. This enables you to dynamically generate content, compute values, and
access variables or props. For example, you can include variables, perform
calculations, or call functions inside JSX expressions.
WEB DEVELOPMENT USING MEAR STACK with
07/10/2024 DEVOPS Unit 1 69
Basic, Understanding JSX
Components as JSX Elements: JSX enables you to use components as JSX elements. When you create a component in
React, you can use it as a custom HTML-like tag in your JSX code. For example, <MyComponent /> represents the
usage of a component named MyComponent.
Class and Style Names: JSX provides support for adding CSS classes and inline styles to elements. Instead of the class
attribute used in HTML, JSX uses the className attribute to specify CSS classes. Inline styles are written using
JavaScript objects with camel-cased property names.
Self-Closing Tags: Just like in HTML, JSX supports self-closing tags for elements that don't have any children. For
example, <img src="image.jpg" alt="Image" /> is a self-closing tag.
Preventing Injection Attacks: JSX automatically escapes content to prevent injection attacks. It ensures that user-
generated content is treated as plain text by default, reducing the risk of Cross-Site Scripting (XSS) vulnerabilities.
Transpilation: JSX code needs to be transpiled to regular JavaScript using tools like Babel to be understood by the
browser. Transpilation converts JSX syntax into equivalent JavaScript function calls that create and manipulate React
elements.
07/10/2024 70
Basic, Understanding JSX
Here's an example of JSX usage in a React component:
return (
<div>
<h1>Hello, {name}!</h1>
<p>This is a JSX example.</p>
</div>
);
};
export default MyComponent;
In this example, the component MyComponent returns JSX elements enclosed within the parentheses. JavaScript
expressions like {name} are used to dynamically render the value of the name variable.
07/10/2024 71
Basic, Understanding JSX
JSX allows you to write more readable and expressive code, making it easier to build and maintain
React components. It combines the power of JavaScript with the flexibility of HTML-like syntax,
enhancing the development experience in React applications.
07/10/2024 72
Basic, Understanding JSX
Nested Elements in JSX
To use more than one element, you need to wrap it with one container element. We use div as a
container element which has three nested elements inside it.
JSX Attributes
JSX use attributes with the HTML elements same as regular HTML.
JSX uses camelcase naming convention for attributes rather than standard naming convention of
HTML such as a class in HTML becomes className in JSX because the class is the reserved keyword
in JavaScript.
We can also use our own custom attributes in JSX. For custom attributes, we need to use data- prefix.
In JSX, we can specify attribute values in two ways:
• As String Literals: We can specify the values of attributes in double quotes.
• As Expressions: We can specify the values of attributes as expressions using curly braces {}.
07/10/2024 73
Basic, Understanding JSX
JSX Comments
JSX allows us to use comments that begin with /* and ends with */ and wrapping them in curly braces
{} just like in the case of JSX expressions.
JSX Styling
React always recommends to use inline styles. To set inline styles, you need to use camelCase syntax.
React automatically allows appending px after the number value on specific elements.
07/10/2024 74
Props and State
React State
The state is an updatable structure that is used to contain data or information about the component.
The state in a component can change over time.
The change in state over time can happen as a response to user action or system event. A component
with the state is known as stateful components.
It is the heart of the react component which determines the behavior of the component and how it will
render. They are also responsible for making a component dynamic and interactive.
A state must be kept as simple as possible. It can be set by using the setState() method and calling
setState() method triggers UI updates.
A state represents the component's local state or information. It can only be accessed or modified
inside the component or by the component directly.
To set an initial state before any interaction occurs, we need to use the getInitialState() method.
Output
this.toggleDisplayBio = this.toggleDisplayBio.bind(this);
output:
Props are immutable so we cannot modify the props from inside the component. Inside the
components, we can add attributes called props. These attributes are available in the component as
this.props and can be used to render dynamic data in our render method.
When you need immutable data in the component, you have to add props to reactDom.render()
method in the main.js file of your ReactJS project and used it inside the component in which you
need. It can be explained in the below example.
The literal difference is that one has state, and the other doesn’t. That means the stateful components are
keeping track of changing data, while stateless components print out what is given to them via props, or
they always render the same thing.
Type out or visualize your website as if it were one component. Busy, right? Break it down from there
into smaller components.
You’ll need state somewhere when information dynamically changes, like a user’s current favorite
songs or top scores. Aim to have a parent component keep all the information, and pass it down to its
stateless children components.
So presentational components can vary depending on what information it receives. The difference is
that a stateful component keeps track of the information itself, instead of just taking it via props and
outputting it.
getDefaultProps()
It is used to specify the default value of this.props. It is invoked before the creation of the component or
any props from the parent is passed into it.
getInitialState()
It is used to specify the default value of this.state. It is invoked before the creation of the component.
Mounting Phase
In this phase, the instance of a component is created and inserted into the DOM. It consists of the
following methods.
componentWillUnmount()
This method is invoked immediately before a component is destroyed and unmounted permanently. It
performs any necessary cleanup related task such as invalidating timers, event listener, canceling network
requests, or cleaning up DOM elements. If a component instance is unmounted, you cannot mount it
again.
componentWillUnmount()
This method is invoked immediately before a component is destroyed and unmounted permanently. It
performs any necessary cleanup related task such as invalidating timers, event listener, canceling network
requests, or cleaning up DOM elements. If a component instance is unmounted, you cannot mount it
again.
Hooks are backward-compatible, which means it does not contain any breaking changes. Also, it does not replace
your knowledge of React concepts.
The above command will install the latest React and React-DOM alpha versions which support React Hooks. Make
sure the package.json file lists the React and React-DOM dependencies as given below.
"react": "^16.8.0-alpha.1",
"react-dom": "^16.8.0-alpha.1",
Side effects have common features which the most web applications need to perform, such as:
react-router is the core package that provides routing capabilities for React applications across different platforms,
while react-router-dom is a package specifically tailored for web applications and extends react-router with
additional components and utilities optimized for web routing.
// Example usage
const App = () => {
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
</BrowserRouter>
);
}
You have now installed React Router or React Router DOM using npm and can start using them in your React
application for routing purposes.
3) What is the command to create a new React application using Create React App?
a) create-react-app my-app
b) npm init react-app my-app
c) react-create-app my-app
d) new-react-app my-app
7) Which method is called immediately after a component is rendered for the first time?
a) componentDidMount
b) componentDidUpdate
c) componentWillMount
d) componentWillRender
Which package is specifically designed for web applications and extends react-router with web-specific
components and utilities?
a) react-router
b) react-router-dom
c) react-router-native
d) react-router-web
1. Explain the concept of component life cycle in React. Provide an overview of the different phases and methods
involved in the life cycle of a React component.
2. Describe the role of props and state in React. Differentiate between the two and explain when it is appropriate to
use props versus state in a component.
3. Discuss the differences between stateful and stateless components in React. When would you choose to use a
stateful component over a stateless component, and vice versa?
4. What is JSX in React? Explain its purpose and advantages in building React applications. Provide an example to
demonstrate the usage of JSX.
5. Compare and contrast the usage of react-router and react-router-dom in a React application. Explain when you
would use one over the other, and provide an example of how you would set up routing using either of the libraries..
• https://youtu.be/pKd0Rpw7O48
• https://youtu.be/TlB_eWDSMt4
• https://youtu.be/QFaFIcGhPoM
• https://youtu.be/EHTWMpD6S0
In a typical React project directory structure, where is the main component usually located?
a) src/
b) public/
c) node_modules/
d) build/
Which of the following is a method that is part of the React component life cycle?
a) renderProps()
b) updateState()
c) componentDidMount()
d) setStateChange()
1. Explain purpose of using a framework in web development? Provide an overview of the benefits and advantages of using frameworks.
2. Explain the difference between local and global NPM packages. How can you install a package globally using NPM?
3. Describe the steps involved in creating a new React application using Create React App.Discuss the typical directory structure of a React
project and explain the purpose of each directory.
4. Discuss about React components? Explain the difference between functional components and class components.
5. Define SX in React? How does JSX differ from regular HTML?
6. Explain the concepts of props and state in React. Provide examples of when you would use each of them.
7. What is the difference between a stateful component and a stateless component in React? In which situations would you prefer to use
one over the other?
8. Describe the lifecycle of a React component and the different phases involved. Provide examples of lifecycle methods and their
purposes.
9. Define React Hooks? Explain the benefits of using Hooks in React development and provide examples of commonly used Hooks.
(2) David Cho, “Full-Stack React, Type Script, and Node: Build cloud-ready web applications
using React 17 with Hooks and GraphQL”, 2nd edition, 2017 Packt Publishing Limited.
(3) Richard Haltman & Shubham Vernekar, “Complete node.js: The fast guide: Learn complete
backend development with node.js”5th edition, 2017 SMV publication.
(4) Glenn Geenen, Sandro Pasquali , Kevin Faaborg, “Mastering Node.js: Build robust and
scalable real-time server-side web applications efficiently” 2nd edition Packt,2017 Publishing
Limited.
Components
Components are independent and reusable block of code. They serve
the same purpose as JavaScript functions, but work in isolation and
return HTML.
Class components:
Class components start with class keyword that
extends the Component constructor from React and has
a render method which returns a JSX.
We used to use class components in order to manage the state. In the older versions of
React (before version 16.8), it was not possible to use state inside functional
components.
Apart from managing the state we use class components to perform some additional
operations, like life-cycle methods.
This has changed with the introduction of React Hooks, and now
we can also manage state and perform life cycle methods inside
functional components with hooks.
Props
Components can be passed as props, which stands for properties.
Props are like function arguments, and you send them into the component
as attributes.