Rules of Hooks
Learn the important rules for using React Hooks correctly in functional components.
CIIT Training Institute | React Tutorial
1. What Are the Rules of Hooks?
React Hooks are special functions such as useState, useEffect, useRef, and useReducer. They allow functional components to use React features.
To make Hooks work correctly, React requires us to follow specific rules. These rules help React identify which state and effects belong to each component.
2. Rule One: Call Hooks at the Top Level
Do not call Hooks inside conditions, loops, or nested functions. Call them at the top level of your component so React can preserve the same Hook order during every render.
Incorrect Example
import { useState } from 'react';
function Student() {
const [isStudent, setIsStudent] = useState(true);
if (isStudent) {
const [name, setName] = useState('Samadhan');
}
return <h2>Student Component</h2>;
}
export default Student;
Here, useState is called inside an
if condition. This breaks the Rules of Hooks.
Correct Example
import { useState } from 'react';
function Student() {
const [isStudent, setIsStudent] = useState(true);
const [name, setName] = useState('Samadhan');
return (
<div>
<h2>Student Details</h2>
<p>Name: {name}</p>
<p>Student: {isStudent ? 'Yes' : 'No'}</p>
</div>
);
}
export default Student;
3. Understanding Hook Order
React relies on the order in which Hooks are called. If that order changes between renders, React may associate state or effects with the wrong Hook.
Keep Hook calls in the same order for every render. You can place conditions inside a Hook's callback or around ordinary logic, but do not conditionally call the Hook itself.
4. Rule Two: Call Hooks Only from React Functions
Hooks should be called from React function components or custom Hooks. Do not call Hooks directly inside ordinary JavaScript functions.
Incorrect Example
import { useState } from 'react';
function getStudentName() {
const [name, setName] = useState('Samiksha');
return name;
}
export default function Student() {
return <h2>Student Details</h2>;
}
The function getStudentName is an ordinary
JavaScript function, not a React component or custom Hook.
Therefore, it must not call useState.
Correct Example
import { useState } from 'react';
function Student() {
const [name, setName] = useState('Samiksha');
return (
<div>
<h2>Student Details</h2>
<p>Name: {name}</p>
</div>
);
}
export default Student;
use,
such as useStudent or useCounter.
5. Using Hooks Inside a Custom Hook
A custom Hook is a JavaScript function whose name starts with
use. It can call other Hooks at its top level.
Custom Hooks help you reuse stateful logic across components.
import { useState } from 'react';
function useStudentName() {
const [name, setName] = useState('Samadhan');
function changeName() {
setName('Samiksha');
}
return { name, changeName };
}
export default useStudentName;
import useStudentName from './useStudentName';
function Student() {
const { name, changeName } = useStudentName();
return (
<div>
<h2>Student Name: {name}</h2>
<button onClick={changeName}>
Change Name
</button>
</div>
);
}
export default Student;
Expected Output
Before clicking: Student Name: Samadhan
After clicking: Student Name: Samiksha
The custom Hook manages the name state and returns the information and function needed by the component.
6. Important Points to Remember
- Call Hooks only at the top level of React components or custom Hooks.
- Never call Hooks inside loops, conditions, or nested functions.
- Call Hooks from React function components or custom Hooks only.
- Do not call Hooks inside ordinary JavaScript functions.
- Start custom Hook names with the word
use. - Keep Hook calls in the same order during every render.
- Use a React Hooks linting rule to help detect violations.
7. Practice Tasks
Complete the following tasks to check your understanding.
- Create a functional component that uses
useStateat the top level. - Identify the error in a component that calls
useStateinside anifcondition. - Create a component that uses both
useStateanduseEffectcorrectly. - Create a custom Hook named
useCounterthat returns a count and an increment function. - Explain why Hooks should not be called inside loops or nested functions.
Summary
React Hooks let functional components use features such as state and effects. To use Hooks correctly, call them only at the top level of React function components or custom Hooks, never inside conditions, loops, or nested functions. Following these rules keeps Hook calls consistent between renders and helps prevent unexpected behavior in React applications.