React Js Unit-I Done
React Js Unit-I Done
Introduction
Workflow
Scope
1. Web Development
ReactJS is ideal for building SPAs where the application loads a single HTML page and
dynamically updates as the user interacts with the app.
ReactJS allows developers to create highly interactive UIs with real-time updates and a
responsive user experience.
c. Component-Based Architecture
2. Mobile Development
a. React Native
ReactJS can be used to develop mobile applications through React Native, allowing
developers to build native apps for iOS and Android using the same React codebase.
a. Next.js
Frameworks like Next.js leverage ReactJS for SSR, improving performance and SEO
by rendering pages on the server before sending them to the client.
ReactJS is used to build PWAs, which offer a native app-like experience on the web
with features like offline access, push notifications, and fast load times.
a. Gatsby
React-based frameworks like Gatsby enable developers to build static websites that are
fast, secure, and can be easily integrated with various data sources.
6. Enterprise Applications
7. E-commerce Platforms
8. Cross-Platform Development
ReactJS, along with tools like Electron, can be used to build cross-platform desktop
applications using web technologies.
ReactJS has a large and active community, contributing to a rich ecosystem of libraries,
tools, and resources that enhance development productivity.
The versatility and robust features of ReactJS make it a powerful tool for a wide range
of development projects, from simple websites to complex, large-scale applications.
Advantages of ReactJS
● Performance
● Easy to learn
● Huge collection of third party components
● Large community
● SEO Friendliness
● Easy kick-starting of the React project
● Rich set of developer tools
● Handle large application
● Simple UI testing
1. Performance
React uses Virtual DOM concept to check and update the HTML document. Virtual
DOM is a special DOM created by React. Virtual DOM represents the real DOM of the
current document. Whenever there is a change in the document, React checks the
updated virtual DOM with the previous state of the Virtual DOM and update only the
different in th actual / real DOM. This improves the performance of the rendering of the
HTML document.
For example, if we create a React component to show the current time by periodically
updating the time through setInterval() method, then React will update only the current
time instead of updating the whole content of the component.
ReactJS is much easier to learn and use. It comes with a good supply of
documentation, tutorials, and training resources. Any developer who comes from a
JavaScript background can easily understand and start creating web apps using React
in a few days. It is the V(view part) in the MVC (Model-View-Controller) model, and
referred to as ?one of the JavaScript frameworks.? It is not fully featured but has the
advantage of open-source JavaScript User Interface(UI) library, which helps to execute
the task in a better manner.
In addition to the core React library (which is only a few KB in size), React community
provides a large number of components for a wide range of applications from simple UI
components to full-fledged PDF Viewer components. React provides multiple options in
each category. For example, the advanced state management can be done using either
Redux or MobX library. Redux and Mobx are just two popular libraries to do state
management. React has more than 10 libraries to archive the same functionality.
Similarly, React community provides lot of third party library in each category like
routing, data grid, calendar, form programming, etc.,
4. Large community
The React developer community is a huge community with a lot of activities. React
community is very active that you can get answer for any react related question / doubts
in a few minutes through google, stackoverflow, etc.,
5. SEO friendliness
React is one of the few JavaScript library to support SEO features. Since React
components and JSX are similar to HTML elements, SEO can be easily achieved
without much code / setup.
React uses composition to merge multiple components into one bigger component,
which in turn allows it to create a much larger component. React components can be
created in a single JavaScript file and can be set as exportable. This feature allows
multiple components to be grouped under a common category as modules and can be
reused in other places.
Composable and modular features of the React library allows developer to create large
applications, which is relatively easy to maintain when compared to other front end
frameworks.
Disadvantages of React
React library has a decent documentation in its primary website. It covers the basic
concept with few examples. Even though, it is a good start to understand the basic of
React concept, it does not provides a deep and detailed explanation with multiple
examples. React community steps in and provides lot of articles with varying level of
complexity and quality. But, they are not organized under one place, where the
developer can easily learn.
React is just a UI library with few concept and standard recommendation. Even through
React can be use to create large / complex application, there is no standard or
recommended way to create a application. As there is no standard way, React
community uses multiple architecture to build their application. Developer are free to
choose a methodology for their application. A wrong choice at the beginning of the
application development complicates the application as well as delay the development
of the application.
React releases new version of the library few times in a year. Each release has few
addition feature and few breaking changes. Developer need to learn fast and apply the
new concept to stabilize the application.
Even through the core concept of the React library is quite simple and easy to learn, the
advanced concept are quite complex as it exploits advanced features of JavaScript.
Also, React has lot of advanced concept to address many of the complex scenarios of
the HTML / Form programming. The shear number of advanced concept is really quite a
challenge for the developer to learn and master it.
5. JavaScript Extension
JSX is an extension of JavaScript language. JSX is similar to HTML and simplifies the
component development. JSX also has few differences with the HTML Programming
and need to be careful to apply it correctly. Additionally, JSX needs to be complied to
JavaScript before executed in the browser which is addition step / burden for the
application.
6. Just a UI library
As we learned earlier, React is just a UI library and not a framework. Creating a React
application with good architecture needs careful selection and application of additional
third party react library. A bad design may affect the application in the later / final stage
of the application development.
Difference between JS and JSX
What is JS?
What is JSX?
JSX, on the other hand, stands for JavaScript XML. It is an extension to JavaScript
syntax that allows you to write HTML-like code within JavaScript. JSX is commonly
used in conjunction with React, a popular JavaScript library for building user interfaces.
JSX provides a more declarative and intuitive way to define the structure and
appearance of components, making it easier to develop and maintain complex UIs. JSX
code is transpiled into regular JavaScript code before it can be executed in the browser.
The notable difference between a .js file and .jsx is that in a .js file, you’re writing
JavaScript and in .jsx file, you’re writing JSX. Of course, you can still write JSX inside a
.js file, but having a separate file for JSX and vanilla JavaScript would reduce
confusion in a large React codebase.
Another difference between a .js and .jsx file is that code editors and their themes
might have different syntax highlighting for JavaScript and JSX. Once you separate the
two in your codebase, things could become easier for you visually.
1. .js Files:
● You can use all the core functionalities of JavaScript in these files, like
2. .jsx Files:
● .jsx files also contain JavaScript code, but with the added ability to use an
HTML-like syntax.
● This allows you to integrate your JavaScript code directly with HTML
elements.
● Components are independent and reusable bits of code. They serve the same
The primary job of a React component is to render its user interface and update it
whenever its internal state is changed. In addition to rendering the UI, it manages
interaction.
State − Enable the component to stay stateful. Stateful component updates its UI
Function Components
Class Components
Class Component
Example
render() {
Function Components
A Function component also returns HTML, and behaves much the same way as a Class
component, but Function components can be written using much less code, are easier
to understand, and will be preferred in this tutorial.
Example
function Car() {
}
Child Components
Child components in ReactJS are components that are nested within parent
components. They are used to build modular and reusable pieces of UI. Here are some
key points about child components in ReactJS:
● Expressions in JSX are javascript expressions that can be used to evaluate and
display dynamic values within JSX code.
● They are defined within curly braces({}).
● These expressions can be any valid javascript expressions,including
variables,function calls and operations.
JSX
Example
root.render(myElement);
Components
● Components are like functions that return HTML elements.
● Components are independent and reusable bits of code.
● They serve the same purpose as JavaScript functions, but work in
isolation and return HTML.
● Components come in two types,
○ Class components
○ Function components
● Components in React serve as independent and reusable code blocks for UI
elements.
● They represent different parts of a web page and contain both structure and
behavior.
● They are similar to JavaScript functions and make creating and managing
complex user interfaces easier by breaking them down into smaller, reusable
pieces.
● A UI is broken down into multiple individual pieces called components.
1.Props
● props are a way to pass the data or properties from parent component to
child components.
● Props are read-only components.
● It is an object which stores the value of attributes of a tag and work similar
to the HTML attributes.
● It allows passing data from one component to other components.
● It is similar to function arguments and can be passed to the component
the same way as arguments passed in a function.
● Props are immutable so we cannot modify the props from inside the
component.
2.State
● state is the real-time data available to use within that only component.
● The state is an updatable structure that is used to contain data or
information about the component and can change over time.
● The change in state can happen as a response to user action or system
events.
● It is the heart of the react component which determines the behavior of
the component and how it will render.
● A state must be kept as simple as possible.
● It represents the component's local state or information.
● It can only be accessed or modified inside the component or by the
component directly.
3. Props allow you to pass data from one State holds information about the
argument.
component. components.
5. Props are used to communicate between States can be used for rendering dynamic
6. Stateless component can have Props. Stateless components cannot have State.
8. Props are external and controlled by The State is internal and controlled by the
Conditional Rendering
Example
function GuestGreeting(props) {
return <h1>Please sign up.</h1>;
}
if(isLoggedIn)
{
return <UserGreeting/>;
}
return <GuestGreeting/>;
root.render(<Greeting isLoggedIn={false}/>);
Advantages of JSX
● JSX enables React to display error and warning messages, which aids in
debugging.
● With JSX, you can write large pieces of code in a more organized and simplified
manner.
● If you have a good understanding of HTML, you'll find it easier to use JSX when
developing React applications.
● It is easy to write HTML text in React using JSX.
● HTML tags can easily be converted to React JS elements.
● The programming language becomes faster than when you use JavaScript.
● You can put HTML elements in DOM without using appendChild() or
createElement().
● XSS (cross-site-scripting) attacks, also known as injection attacks, can be
prevented with the use of JSX.
● You can detect most of the errors during the compilation time only.
● Being an expression, JSX can be used inside ‘if statements’ and ‘for loops’. It is
also possible to assign this expression to variables and accept it as arguments.
Expressions in JSX
● Expressions in JSX are JavaScript expressions that can be used to evaluate and
display dynamic values within JSX code.
● They are defined within curly braces ({}).
● These expressions can be any valid JavaScript expressions, including variables,
function calls, and operations.
● Any statement that can return a value is automatically qualified to be used as an
expression.
● An expression is a combination of values, variables, and operators, which
computes to a value.
Implementation of JSX
JSX (JavaScript XML) is a syntax extension for JavaScript used with React to describe
what the UI should look like. It resembles HTML but comes with the full power of
JavaScript.
First, ensure you have Node.js and npm installed. You can create a new React project
using Create React App:
Inside your project, you can create a new component using JSX. For example, create a
HelloWorld component.
src/components/HelloWorld.js
You can now use the HelloWorld component in your main App.js file.
src/App.js
import React from 'react';
import HelloWorld from './components/HelloWorld';
function App() {
return (
<div className="App">
<header className="App-header">
<HelloWorld />
</header>
</div>
);
}
npm start
This will start the development server and open your app in a browser. You should see
"Hello, World!" displayed on the page.
Environment Setups
1.Install NodeJS and NPM.NodeJS and NPM are the platforms need to develop any
ReactJS applications.
2. install React and React DOM.
3.install webpack
4.install babel
5.create files
6.configure webpack
7.webpack.config.json
Here are Some steps you can take to set up a React.Js development
environment.
The easiest way to create a package. json file is to run npm init to
generate one for you.
Visual Studio Code, also known as VS Code, is a highly versatile and popular IDE
among React developers. It offers built-in support for JavaScript and React, an
extensive marketplace of plugins, and features such as code completion, React Router
integration, and debugging. It is available for Windows, Mac, and Linux.
● Mounting: When a component is first created and inserted into the DOM.
● Updating: When a component's state or props change.
● Unmounting: When a component is removed from the DOM.
1.Initialization:
The is the stage where the component is constructed with the given props and
default state.This is done in the constructor of a component class.
2.Mounting:
Mounting is the stage of rendering the JSX returned by the render method itself.
3.Updating:
Updating is the stage when the state of a component is updated and the
application is repainted.
4.unmounting:
As the name suggests unmounting is the final step of the component lifecycle
where the component is removed from the page.