React Fundamentals
React Fundamentals
FUNDAMENTALS
WHAT IS REACT JS?
• React is a JavaScript library created by Facebook
• React is a User Interface (UI) library
• It is an open source, component based front end
library which is responsible only for the view layer of
the application.
• It is not a framework.
• It is a very in demand skillset used in companies.
• React’s components are reusable which makes it
easier to develop and maintain your apps.
• Text Editor: VS Code
• Install : Node JS
REACT JS INSTALLATION:
index.js
This is the top renderer of your react app. In
the index.js file, we import React, ReactDOM, and
the CSS file.
node_modules
All the packages installed by NPM will reside inside
the node_modules folder.
App.js
The App.js file contains the definition of
our App component which actually gets rendered in the
browser and this is the root component.
COMPONENTS:
Components are the core building blocks of React
applications.
They are logical group of code that make up the entire
application.
With the help of components, you can make UIs quickly.
Every component works independently, and you can
merge them into a parent component.
Components are like functions that return HTML
elements.
Components are independent and reusable bits of code.
There are two types of Components in ReactJS:
1. Functional Components
2. Class Components
FUNCTIONAL COMPONENTS:
Functional Components are simply JavaScript functions.
We can create a functional component in React by
writing a JavaScript function.
These functions may or may not receive data as
parameters.
In the functional Components, the return value is the
JSX(JSX allows us to write HTML in React) code to render to the
DOM tree.
Function components can be written using much less
code, are easier to understand, and is preferred over
class components.
FUNCTIONALCOMPONENT.JS FILE
APP.JS FILE
OUTPUT:
FUNCTIONALCOMPONENT.JS
FILE
APP.JS FILE
OUTPUT:
CLASS COMPONENTS:
Class components are simple classes (made up of
multiple functions that add functionality to the
application).
A class component must 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.
CLASSCOMPONENT.JS FILE
APP.JS FILE
OUTPUT:
REACTJS PROPS:
props stands for properties.
Props are arguments passed into React components.
• getDerivedStateFromProps() =
This method is called right before rendering the
element(s) in the DOM.
It takes state as an argument, and returns an object
with changes to the state.
• Render =
The render() method is required, and is the method that
actually outputs the HTML to the DOM.
REACTJS-COMPONENT LIFECYCLE:
Updating Phase:
The next phase in the lifecycle is when a component is
updated.
A component is updated whenever there is a change in
the component's state or props.
React has five built-in methods that gets called, in this
order, when a component is updated:
getDerivedStateFromProps()
shouldComponentUpdate()
render()
getSnapshotBeforeUpdate()
componentDidUpdate()
REACTJS-COMPONENT LIFECYCLE:
Updating Phase:
getDerivedStateFromProps
This is the first method that is called when a component
gets updated.
This is the natural place to set the state object based on
the initial props.
shouldComponentUpdate
In the shouldComponentUpdate() method you can
return a Boolean value that specifies whether React
should continue with the rendering or not.
The default value is true.
REACTJS-COMPONENT LIFECYCLE:
Updating Phase:
render
The render() method is called when a component gets
updated, it has to re-render the HTML to the DOM, with
the new changes.
getSnapshotBeforeUpdate
In the getSnapshotBeforeUpdate() method you have
access to the props and state before the update,
meaning that even after the update, you can check what
the values were before the update.
componentDidUpdate
The componentDidUpdate method is called after the
component is updated in the DOM.
REACTJS-COMPONENT LIFECYCLE:
Unmouting Phase:
The next phase in the lifecycle is when a component is
removed from the DOM, or unmount.
componentWillUnmount =
The componentWillUnmount method is called when the
component is about to be removed from the DOM.
REACTJS-EVENTS:
Events are actions that are triggered by system
generated events or user actions.
React’s event handling system is known as synthetic
events.
function MyForm() {
return (
<form>
<label>Enter your name:
<input type="text" />
</label>
</form>
)
}
REACTJS-ROUTER:
When a user is directed to a page based on
their action or request, the process is called
routing.
React Router is a popular library for handling
routing and navigation in React applications.
It allows you to create single-page applications
(SPAs) with multiple views or pages that can
be navigated through without the need to
reload the entire page.
React Router is often used to build dynamic
and interactive user interfaces in React
applications.
REACTJS-ROUTER:
Example:
function App() {
return (
<Router>
<div>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
</div>
</Router>
);
}
export default App;
REACTJS-ROUTER:
In this example:
You can now create links to navigate between these routes. For
example, in your Home component, you can add a link to the About
page: