Explain the new feature of React hooks introduced in React v16.8
Last Updated :
01 Aug, 2024
React v16.8 is no less than a miracle for all the React developers because this version of React introduced hooks to all of us. Before its release, the developer community was tired of using class components but they still had to use them because the functional component does not provide many features that the class component provides. But after hooks, we can use those features in the functional component that was only accessible with the class component. So we can say after this version i.e. React v16.8, the popularity of the class component was reduced.
They are like the functions that help you hook into the state and lifecycle of React from the functional component. Hooks can not be used with the class component and it only works inside the functional component.
Hooks can only be called at the top of any functional component. Never ever try to call hooks inside any condition, loop, or nested functions. Hooks can be easily identified inside any functional component because they start with the "use" keyword.
There are two types of Hooks:
- built-in hooks: These are the hooks that are built-in and we have to only use those to utilize their features. Some examples of built-in hooks are "useEffect", "useMemo", "useRef" etc...
- Custom hooks: It is simply a javascript function whose name starts with the "use" keyword and it can call other hooks. In other words, custom hooks are those hooks that we have to make to get the required features.
Now for using hooks, we have to import hooks in those files where we want to use hooks.
Syntax:
- importing built-in hooks:
import { hook1,hook2,hook3.... } from 'react';
Here "hook1,hook2,hook3........." represents different hooks separated by commas that you want to import in your react component.Â
import HookName from 'filepath';
Here "HookName" is the name of the custom hook and "filepath" is the location of the file in which that hook is located
Steps to Create React Application:
Step 1: Create a React application using this command:
npx create-react-app reacthook
Step 2: After creating your project folder i.e. reacthook, move to it using this command:
cd reacthook
Project Structure:
Project StructureNew features of hooks in React v16.8:
1. Hooks allow us to use State inside functional Component:
We all are aware of React State. It has a very big advantage over any variable because if the variable got changed then the component will not re-render but if somehow the state got changed then the whole component will re-render.
Before React v16.8 we can only use state with class component because there "state" is a built-in object. But after this version of react, a functional component can also use state via the "useState" hook.
Syntax:
let [current state, function] = useState(Initial value of that state);
Example: In the below example we have created a state "work" whose initial value was "learning" and on button click we have changed that value. Since it is state and its value got changed that's why it re-rendered the whole component and we can see the updated value.
JavaScript
// Filename - App.js
import "./App.css";
import { useState } from "react";
function App() {
let [work, setWork] = useState("Learning");
function updateWork() {
setWork("Learning from GFG");
}
return (
<div className="App">
<header className="App-header">
<h1 className="gfg">GeeksforGeeks</h1>
<h3 className="gfghook">
State in Functional Component
</h3>
<h3>{work}</h3>
<button onClick={updateWork}>Click Here</button>
</header>
</div>
);
}
export default App;
Steps to Run the Application: Run your application using the following command:
npm start
Output:
.gif)
2. Hooks help us to use the life cycle features of React in functional component:
There is a hook called "useEffect" that helps us to get React life cycle feature which was only available with the class components. "useEffect" hook is like the combination of "componentDidMount", "componentDidUpdate", "componentWillUnmount". It means this hook will be called on mounting, updating the component, and before unmounting the component.
Syntax:
useEffect(function,array of dependencies)
Note: Here array of dependencies is optional
Example: In the example given below, the useEffect hook is called on the mounting of the component and after that, it will be called as many times as we update that component.
JavaScript
import "./App.css";
import { useState, useEffect } from "react";
function App() {
let [count, setCount] = useState(0);
function updateCount() {
setCount((count = count + 1));
}
useEffect(() => {
console.log("Component is mounted,updated or unmounted");
});
return (
<div className="App">
<header className="App-header">
<h1 className="gfg">GeeksforGeeks</h1>
<h3 className="gfghook">
LifeCycle features in Functional Component</h3>
<h3>Count is {count}</h3>
<button onClick={updateCount}>Click Here</button>
</header>
</div>
);
}
export default App;
Steps to Run the Application: Run your application using the following command:
npm start
Output:
Note: After React 18 "useEffect" is called two times on mounting when we are using "StrictMode" in our "index.js" file. So since we are using "StrictMode" in our "index.js" file that's why we are getting output accordingly
3. Hooks help us to use pure component features in functional component:
Suppose there is a state which contains any value, let us say 0. Now there is a function that updates the value of the state but the new value and previous value of the state remain the same. This means that the function replaced 0 with 0. In that case, the component will re-render because states get updated.
But in reality, the updated value is the same as the previous value. So there is no such benefit of re-rendering. Therefore, React has a component called pure component which compares the previous value of state and props with the updated value. And if the updated value is the same as the previous value then it will not re-render itself and if an updated value is different from the previous value then it will re-render itself.
But before React v16.8 this feature of pure component is only accessible with class components but now we can access this feature in functional components via the "useMemo" hook.
Syntax:
useMemo(function,array of dependencies)
useMemo hook is called for the first time on mounting and after that, it is called when any of its dependencies really update means its previous value is different from the updated value.
Now we will return the code of the component from useMemo that we want to re-render only on the actual updation of any dependency. After returning we will store that in any variable and place that in the return statement of the functional component
Example:
JavaScript
// Filename - App.js
import { useState, useMemo } from "react";
function App() {
let [count, setCount] = useState(0);
function updateCount() {
setCount((count = count + 1));
}
let a = useMemo(() => {
console.log("State really got updated");
return (
<>
<h1 className="gfg">GeeksforGeeks</h1>
<h3 className="gfghook">
Pure Component features in Functional Component
</h3>
<h3>Count is {count}</h3>
<button onClick={updateCount}>Click Here</button>
</>
);
}, [count]);
return (
<div className="App">
<header className="App-header">{a}</header>
</div>
);
}
export default App;
Steps to Run the Application: Run your application using the following command:
npm start
Output:
4. Hooks help us to manipulate DOM
Earlier DOM Manipulation was possible with createRef() method of React. But this can be done in class component only. Now, after React v16.8 we can manipulate DOM via the "useRef" hook.
Syntax:
useRef(initial value)
For DOM Manipulation we should initialize useRef with null and store that in any variable. After that where ever we have to use that useRef we will simply write:
ref={variable name in which useRef is stored}
So in the below example, we will manipulate DOM by input field so added ref={myref} in the input field.
After that, we added functionality in a function using "useRef" to show "1000" in that input field and to make the background color yellow of that input field. Then we called that function on a button click.
Example:Â
JavaScript
// Filename - App.js
import "./App.css";
import { useRef } from "react";
function App() {
let myref = useRef(null);
function handleDOM() {
myref.current.value = 1000;
myref.current.style.backgroundColor = "yellow";
}
return (
<div className="App">
<header className="App-header">
<h1 className="gfg">GeeksforGeeks</h1>
<h3 className="gfghook">
DOM Manipulation in Functional Component</h3>
<input type="text" ref={myref}></input>
<button onClick={handleDOM}>Click Here</button>
</header>
</div>
);
}
export default App;
Steps to Run the Application: Run your application using the following command:
npm start
Output:
Similar Reads
Explain new features of React Memo in React v16.6 ?
React memo is a Higher Order Component (HOC) introduced in React v16.6. As the name suggests, Memo API uses the memoization technique to optimize performance and make the application faster. The Memo API avoids unnecessary re-renders in functional components thereby optimizing the performance of the
2 min read
New Features of strict Mode in React 18
React 18 introduces significant enhancements, especially in strict mode, aimed at improving development experiences. It offers comprehensive warnings to catch common mistakes early, along with improved component stack traces for easier debugging. These enhancements emphasize early error detection an
6 min read
Why to use React Hooks Instead of Classes in React JS ?
The introduction of React Hooks has changed the way we are managing states and lifecycle features. They offer more easy and functional way as compared to class based components. In this article, we will learn why to use React Hooks Instead of Classes in ReactJS, but lets first discuss about both Rea
4 min read
Explain the concept of "lifting up in React Hooks"
"Lifting State Up" is a concept in React that involves moving the state of a component higher up in the component tree. In React, the state is typically managed within individual components, but there are situations where multiple components need to share and synchronize the same state. When this ha
2 min read
What are the rules of hooks in React, and why are they important?
In React, hooks empower functional components to utilize state and incorporate other React functionalities seamlessly, enabling a more dynamic and concise development approach. Rules of Hooks in React:Only use hooks at the top level: This means don't use hooks inside loops, conditions, or nested fun
2 min read
What are the React Router hooks in React v5?
React Router hooks perform client-side single-page routing that provides a functional and streamlined approach to managing navigation in React applications. It provides a way to directly access the state of the router from functional components, this simplifies tasks like retrieving information abou
5 min read
How to set an object key inside a state object in React Hooks?
We can update a React hooks state object that has a nested object containing objects with index keys with the following approach, Before doing so, consider the following example: Example: Following is the default state object: const [data, setData] = useState({ name:'', contact:'', address:{ 0:{}, 1
2 min read
What are the benefits of using hooks in React-Redux?
Have you ever wondered how users create amazing websites and apps? Well, in the world of programming, they have some cool tools, and today we're going to explore one of them called "Hooks" in the superhero team of React-Redux. Prerequisites:ReactReact-ReduxReact HooksJavaScriptWhat are Hooks?Hooks a
2 min read
React 19 : New Features and Updates
React 19 is officially released on April 25, 2024, marking a significant milestone. This release brings various new features and improvements to enhance developer experience and application performance. Many experimental features in React 18 are now considered stable in React 19, offering a more rob
13 min read
State Management in React â Hooks, Context API and Redux
State management is a critical concept when working with React. React components can hold local state, but as applications grow, managing state across multiple components can become complex. To help manage this complexity, React provides several tools: Hooks, Context API, and Redux. Here are some fe
6 min read