A React Hook is a special function—always named with a use prefix such as useState or useEffect—that lets function components tap into React state, DOM refs, context, and lifecycle synchronization. Before Hooks arrived in React 16.8, stateful logic forced developers to rewrite functions into verbose class components.
| Hook | Purpose |
|---|---|
useState |
Store and update local component state |
useEffect |
Sync with external APIs, timers, or browser events |
useContext |
Read shared Context values without prop drilling |
useRef |
Hold mutable values or DOM element references |
useReducer |
Manage structured state transitions with actions |
useTransition |
Keep UI responsive during heavy non-urgent updates |
import { useState, useEffect } from 'react';
export function DocumentTitleCounter() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `Count: ${count}`;
}, [count]);
return (
<button type="button" onClick={() => setCount((c) => c + 1)}>
Increment ({count})
</button>
);
}
React tracks Hook values internally using an array indexed by call order. Breaking call order breaks state alignment across renders, so follow two strict rules:
if conditions, for loops, nested callbacks, or after an early return.useSomething), never in regular utility functions.if (!user) return null; before useEffect(...) shifts Hook order when user logs in and crashes React.eslint-plugin-react-hooks Disabled: Keep the official React Hooks ESLint plugin active so missing dependencies and conditional hook calls are caught automatically.No. Hooks only work inside function components and custom Hooks. To reuse Hook logic in an old class component, wrap the class in a small function component and pass props down.
use?The use prefix lets React's linter verify Hook call rules automatically and signals to teammates that the function follows Hook constraints.
Yes. Extracting useState and useEffect into a function named useFetch or useDebounce lets you share stateful behavior cleanly across components.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
You've completed this section! Take a quick 5-question quiz to check your understanding.