Module 6
Module 6
Advance React
Functional components
React Refs
• Refs is the shorthand used for references in React.
• It is similar to keys in React.
• It is an attribute which makes it possible to store a reference to particular DOM nodes
or React elements.
• It provides a way to access React DOM nodes or React elements and how to interact
with it.
• It is used when we want to change the value of a child component, without making the
use of props.
In React, when a ref is passed to an element inside render method, a reference to the
node can be accessed via the current attribute of the ref.
focus()
focus() method tells the browser which element is being acted on
Add Ref to DOM elements
In the below example, we are adding a ref to store the
reference to a DOM node or element.
render() {
return (
import React, { Component } from 'react';
<div>
import { render } from 'react-dom';
<h2>Adding Ref to DOM element</h2>
<input
class App extends React.Component {
type="text"
constructor(props) {
ref={this.callRef} />
super(props);
<input
this.callRef = React.createRef();
type="button"
this.addingRefInput =
value="Add text input"
this.addingRefInput.bind(this);
onClick={this.addingRefInput}
}
/>
</div>
addingRefInput() {
);
this.callRef.current.focus();
}
}
}
export default App;
Add Ref to Class components
In the below example, we are adding a ref to store
the reference to a class component.
Example
import React, { Component } from 'react'; class App extends React.Component {
import { render } from 'react-dom'; constructor(props) {
super(props);
function CustomInput(props) { this.callRefInput = React.createRef();
let callRefInput = React.createRef(); }
The callback function is used to store a reference to the DOM node in an instance
property and can be accessed elsewhere. It can be accessed as below:
this.callRefInput.value
Hooks
React Hooks
• Hooks are the new feature introduced in the React 16.8 version.
• It allows you to use state and other React features without writing a class.
• Hooks are the functions which "hook into" React state and lifecycle features from
function components.
• It does not work inside classes.
• Hooks are backward-compatible, which means it does not contain any breaking
changes.
• Also, it does not replace your knowledge of React concepts.
• If you write a function component, and then you want to add some state to it,
previously you do this by converting it to a class.
• But, now you can do it by using a Hook inside the existing function component.
Rules of Hooks
Hooks are similar to JavaScript functions, but you need to follow these two rules when
using them. Hooks rule ensures that all the stateful logic in a component is visible in its
source code. These rules are:
The above command will install the latest React and React-DOM alpha versions which
support React Hooks.
Hooks State
Hook state is the new way of declaring a state in React app. Hook uses useState() functional
component for setting and retrieving state. Let us understand Hook state with the following
example.
App.js
function CountApp() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
export default CountApp;
output:
Hooks Effect
• The Effect Hook allows us to perform side effects (an action) in the function
components.
• It does not use components lifecycle methods which are available in class components.
• In other words, Effects Hooks are equivalent to componentDidMount(),
componentDidUpdate(), and componentWillUnmount() lifecycle methods.
Side effects have common features which the most web applications need to perform,
such as:
function CounterExample() {
const [count, setCount] = useState(0);
The above code is based on the previous example with a new feature which we set the
document title to a custom message, including the number of clicks.
In React component, there are two types of side effects:
Basic Hooks
• useState
• useEffect
• useContext
Additional Hooks
• useReducer
• useCallback
• useMemo
• useRef
• useImperativeHandle
• useLayoutEffect
• useDebugValue
React useEffect Hooks
• The useEffect Hook allows you to perform side effects in your components.
• Some examples of side effects are: fetching data, directly updating the DOM,
and timers.
useEffect(<function>, <dependency>)
Let's use a timer as an example.
Example:
Use setTimeout() to count 1 second after initial render:
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
setTimeout(() => {
setCount((count) => count + 1);
}, 1000);
});
• useEffect runs on every render. That means that when the count changes, a render
happens, which then triggers another effect.
• This is not what we want. There are several ways to control when side effects run.
• We should always include the second parameter which accepts an array. We can
optionally pass dependencies to useEffect in this array.
1. No dependency passed:
useEffect(() => {
//Runs on every render
});
2. An empty array:
useEffect(() => { //Runs only on the first render }, []);
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
setTimeout(() => {
setCount((count) => count + 1);
}, 1000);
}, []); // <- add empty brackets here
function Counter() {
const [count, setCount] = useState(0);
const [calculation, setCalculation] = useState(0);
useEffect(() => {
setCalculation(() => count * 2);
}, [count]); // <- add the count variable here
return (
<>
<p>Count: {count}</p>
<button onClick={() => setCount((c) => c + 1)}>+</button>
<p>Calculation: {calculation}</p>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Counter />);
Effect Cleanup
• Some effects require cleanup to reduce memory leaks.
• Timeouts, subscriptions, event listeners, and other effects that are no longer needed
should be disposed.
• We do this by including a return function at the end of the useEffect Hook.
Example:
Clean up the timer at the end of the useEffect Hook:
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
let timer = setTimeout(() => {
setCount((count) => count + 1);
}, 1000);
return () => clearTimeout(timer)
}, []);
return <h1>I've rendered {count} times!</h1>;}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Timer />);
React Flux Concept
• Flux is an application architecture that Facebook uses internally for building the client-
side web application with React.
• It is not a library nor a framework.
• It is a kind of architecture that complements React as view and follows the concept of
Unidirectional Data Flow model.
• It is useful when the project has dynamic data, and we need to keep the data updated
in an effective manner.
• It reduces the runtime errors.
• Dispatcher
• Stores
• Views (React components)
Structure and Data Flow
• In Flux application, data flows in a single direction(unidirectional).
• This data flow is central to the flux pattern.
• The dispatcher, stores, and views are independent nodes with inputs and outputs.
• The actions are simple objects that contain new data and type property.
• Now, let us look at the various components of flux architecture one by one.
Dispatcher
• It is a central hub for the React Flux application and manages all data flow of your Flux
application.
• It is a registry of callbacks into the stores.
• It has no real intelligence of its own, and simply acts as a mechanism for distributing
the actions to the stores.
• All stores register itself and provide a callback.
• It is a place which handled all events that modify the store.
• When an action creator provides a new action to the dispatcher, all stores receive that
action via the callbacks in the registry.
The dispatcher's API has five methods. These are:
SN Methods Descriptions
Views
• It is also called as controller-views.
• It is located at the top of the chain to store the logic to generate actions and receive
new data from the store.
• It is a React component listen to change events and receives the data from the stores
and re-render the application.
Actions
• The dispatcher method allows us to trigger a dispatch to the store and include a
payload of data, which we call an action.
• It is an action creator or helper methods that pass the data to the dispatcher.
Advantage of Flux
• It is a unidirectional data flow model which is easy to understand.
• It is open source and more of a design pattern than a formal framework like MVC
architecture.
• The flux application is easier to maintain.
• The flux application parts are decoupled.
Model View Controller framework
• MVC stands for Model View Controller.
• It is an architectural pattern used for developing the user interface.
• It divides the application into three different logical components: the Model, the View,
and the Controller.
• It is first introduced in 1976 in the Smalltalk programming language.
• In MVC, each component is built to handle specific development aspect of an application.
• It is one of the most used web development frameworks to create scalable projects.
MVC Architecture
The MVC architecture contains the three components. These are:
• Controller: It acts as an interface between the Model and the View components. It
takes user input, manipulates the data(model) and causes the view to update.
SN MVC FLUX
1. It was introduced in 1976. It was introduced just a few years ago.
2. It supports Bi-directional data Flow model. It supports Uni-directional data flow model.
3. In this, data binding is the key. In this, events or actions are the keys.
4. It is synchronous. It is asynchronous.
5. Here, controllers handle everything(logic). Here, stores handle all logic.
6. It is hard to debug. It is easy to debug because it has common initiating point: Dispatcher.
8. Its maintainability is difficult as the project scope goes Its maintainability is easy and reduces runtime errors.
huge.
App.js
console.log(add(16, 26)); // 42
math.js
function add(a, b) {
return a + b;
}
console.log(add(16, 26)); // 42
• As our app grows, our bundle will grow too, especially when we are using large third-
party libraries.
• If the bundle size gets large, it takes a long time to load on a webpage.
• For avoiding the large bundling, its good to start splitting your bundle.
• React 16.6.0, released in October 2018, and introduced a way of performing code
splitting.
• Code-Splitting is a feature supported by Webpack and Browserify, which can create
multiple bundles that can be dynamically loaded at runtime.
Code splitting uses React.lazy and Suspense tool/library, which helps you to load a
dependency lazily and only load it when needed by the user.
Before
import ExampleComponent from './ExampleComponent';
function MyComponent() {
return (
<div>
<ExampleComponent />
</div>
);
}
After
const ExampleComponent = React.lazy(() => import('./ExampleComponent'));
function MyComponent() {
return (
<div>
<ExampleComponent />
</div>
); The above code snippet automatically loads the bundle which contains the
} ExampleComponent when the ExampleComponent gets rendered.
Suspense
• If the module which contains the ExampleComponent is not yet loaded by the
function component(MyComponent), then we need to show some fallback content
while we are waiting for it to load.
• In other words, the suspense component is responsible for handling the output when
the lazy component is fetched and rendered.
const ExampleComponent = React.lazy(() => import('./ ExampleComponent'));
function MyComponent() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<ExampleComponent />
</Suspense>
</div>
);
}
• The fallback prop accepts the React elements which you want to render while waiting for
the component to load.
• We can combine multiple lazy components with a single Suspense component.
• It can be seen in the next example.
const ExampleComponent = React.lazy(() => import('./ ExampleComponent'));
const ExamComponent = React.lazy(() => import('./ ExamComponent'));
function MyComponent() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<section>
<ExampleComponent />
<ExamComponent />
</section>
</Suspense>
</div>
);
}
Error boundaries
• If any module fails to load, for example, due to network failure, we will get an error.
• We can handle these errors with Error Boundaries.
• Once we have created the Error Boundary, we can use it anywhere above our lazy
components to display an error state.
ExampleComponents.js
MyFirstComponent.js
MyApp.js
• It is made primarily for JavaScript, but it can transform front-end assets such as HTML,
CSS, and images if the corresponding loaders are included.
• Webpack takes modules with dependencies and generates static assets representing
those modules.
Module Bundler:
A Task Runner: