Things You Should Know About React Hooks
Last Updated :
23 Jul, 2025
React...We all know the importance of this library in the tech industry. Most of the applications are switching to React because of its advantages and features. There are many features of React. React hooks is one of them. React hooks was first released in October 2018. In React a lot of developers use the lifecycle method which is nothing, but just a series of components. It is used from the birth of React component to its death.
render(), componentDidMount(), componentDidUpdate() componentWillUnmount() all these are the lifecycle method. React hooks is the alternative approach of state management and lifecycle method. There are many hooks used for different purposes. Some of them are useReducer, useState, useCallBack, useMemo, useRef, useDispatcher etc.
In this blog, we will discuss the common hooks, the benefits of React hooks, the rules of React hooks along some examples.
Benefits of Using React Hooks
Hooks provide a lot of benefits to the developers. It makes your component better, and it helps in writing clear, concise, and maintainable code. It just cut all the unnecessary code from your component and makes your code more readable. But the question is when to use React hooks?
Use Hooks when you're writing a function component, and you want to add some state to it. Earlier this job was done by using a Class, but now you can write the hooks inside a function component.
Rules of Hook
Below are the main rules of using React hooks...
1. Always call hooks at the top level. Do not call it inside loops, conditions, or nested functions. You will be ensured that hooks can be called in the same order each time component renders.
2. Hooks can not be called from regular JavaScript functions. You can call it from React function components. One hook can call another hook.
Hooks Effect
Hooks effect allows you to perform a side effect in function components. Hooks effect has no use of function components available in-class components. Hooks effects are similar to the lifecycle method componentDidMount(), componentDidUpdate(), and componentWillUnmount().
Hooks effect has the common features given below...
- Updating the DOM
- Fetching and Consuming data from server API
- Setting up subscription
Below is one of the examples of React hooks.
JavaScript
import React, { useState, useEffect } from 'react';
function CounterExample() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
const incerementCount = () =>setCount(count+1);
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={incrementCount}>
Click me
</button>
</div>
);
}
export default CounterExample;
Context Hook
There is a concept of Context hook in React. Context hook is used in Context API. You create the context in React and this hook is used to interact with created context. An example is given below...
JavaScript
import React, { useContext } from 'react';
const ExampleContext = React.createContext();
function Display() {
const Examplevalue = useContext(ExampleContext);
return <div>{Examplevalue}, This value is from context.</div>;
}
function App() {
return (
<ExampleContext.Provider value={"Tamil"}>
<Display />
</ExampleContext.Provider>
);
}
Reducer Hook
You might have listened to this word if you have worked with Redux. Reducer hook works as an alternative for a state hook. When a state is changed it gets bundled in a central function called Reducer. After that state will be updated depending on the action and existing state. One of the examples is given below... You can rewrite the state hook example as given below...
JavaScript
import React, { useReducer } from 'react';
const initialState = {count: 0};
function reducer(state, action) {
switch (action.type) {
case 'increment': return { count: state.count + 1 };
case 'decrement': return { count: state.count - 1 };
default: throw new Error();
}
}
function App() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
<h2>Count: {state.count}</h2>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
</>
);
}
Ref Hook
Reference hook refers to the React element created by the render method. An example is given below...
JavaScript
import React, { useRef } from 'react';
function App() {
const newElement = useRef(null);
const onButtonClick = () => {
newElement.current.focus();
};
return (
<>
<input ref={newElement} type="text" />
<button onClick={onButtonClick}>Focus to Element</button>
</>
);
}
Final Thought
We have discussed the major hooks and their usage with code examples. These hooks are not limited here. Once you will have experience in React, you will be using many more hooks such as useDispatch, useSelect, etc. Each one of them has its own application.
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides DOM-specific methods to interact with and manipulate the Document Object Model (DOM), enabling efficient rendering and management of web page elements. ReactDOM is used for: Rendering Components: Displays React components in the DOM.DOM Manipulation: Al
2 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React projects involve displaying data collectionsâlike user profiles, product catalogs, or tasksâunderstanding how to work with lists.To render a
4 min read
React FormsIn React, forms are used to take input from users, like text, numbers, or selections. They work just like HTML forms but are often controlled by React state so you can easily track and update the input values.Example:JavaScriptimport React, { useState } from 'react'; function MyForm() { const [name,
4 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. When rendering a list, you need to assign a unique key prop to each element in th
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects