Menu

React Hooks: Every Hook Explained with Live Examples

A hook is a function whose name starts with use that lets a component use a React feature such as state, context, refs or effects. See every built-in hook grouped by purpose, the two rules every hook follows, and how to write your own.

This page includes runnable editors - edit, run, and see output instantly.

React hooks are functions whose names start with use that let a function component use React features: useState to remember a value, useEffect to run code after rendering, useRef to hold onto a DOM node, useContext to read shared data. You call them at the top of your component, and React keeps their values between renders.

Four hooks work together here. useState stores the text, useRef points at the input so the button can focus it, useMemo keeps the filtered list until the query changes, and useEffect logs after each render where the query changed. Type an and watch the Console: one line per keystroke, after the list updates.

Why hooks exist

Before React 16.8, a component that needed state or lifecycle code had to be a class. The logic for one feature was split across componentDidMount, componentDidUpdate and componentWillUnmount, and sharing it between components meant mixins (which never worked with class syntax), higher order components or render props, all of which wrapped your tree in extra layers.

// The class way: one feature, three methods
class Clock extends React.Component {
    state = { now: new Date() };
    componentDidMount() {
        this.id = setInterval(() => this.setState({ now: new Date() }), 1000);
    }
    componentWillUnmount() {
        clearInterval(this.id);
    }
    render() {
        return <p>{this.state.now.toLocaleTimeString()}</p>;
    }
}

With hooks the same feature lives in one place: the effect starts the timer and its cleanup stops it.

Hide the clock and the Console prints timer stopped: React ran the cleanup when Clock left the screen. Show it again and a fresh timer starts. Class components still work in React 19, but new code uses hooks.

Every built-in hook

React 19.2 groups its hooks by what they give a component. You will use the first two groups every day and the rest when a specific problem calls for them.

HookWhat it gives youPage
useStateA value that survives renders and a setter that renders againuseState
useReducerState updated by a reducer function and dispatched actionsuseReducer
useContextA value provided by a parent, without passing propsuseContext
useRefA mutable box that survives renders, often a DOM nodeuseRef
useImperativeHandleChooses what a parent sees through your refforwardRef
useEffectCode that usually runs after paint: timers, subscriptions, fetchinguseEffect
useLayoutEffectCode that runs after the DOM updates but before paintuseLayoutEffect
useInsertionEffectRuns before layout effects, for CSS-in-JS libraries
useEffectEventA function an effect can call that always sees the latest props and stateuseEffect
useMemoCaches a computed value between rendersuseMemo
useCallbackCaches a function between rendersuseCallback
useTransitionMarks an update as non urgent and tells you while it is pendinguseTransition
useDeferredValueA copy of a value that lags behind during heavy rendersuseDeferredValue
useIdA unique id that matches on the server and the clientuseId
useSyncExternalStoreSubscribes to a store that lives outside ReactuseSyncExternalStore
useDebugValueA label for a custom hook in React DevTools
useReads a promise or a context, and may sit inside a conditionuse
useActionStateState that a form action updates, with a pending flaguseActionState
useOptimisticShows the expected result before the server answersuseOptimistic
useFormStatusThe pending state of the parent form (from react-dom)

use, useActionState, useOptimistic and useFormStatus arrived with React 19, and useEffectEvent became stable in 19.2. Every other hook on the list works in React 18 too.

State renders, refs do not

Not every hook triggers a render. useState and useReducer do: change their value and React draws the component again. useRef hands you an object whose current field you can change freely, and React never re-renders because of it. That makes it the right place for a timer id, a previous value, or a count the screen does not show.

useReducer holds the cart and useState holds the note; both cause a render when they change, and the effect logs one line per render. The effect also changes commits.current, yet that change never causes a render of its own, so the log does not loop. The count is updated in an effect rather than in the component body because the body should not change anything while it renders.

The rules of hooks

Every hook follows two rules:

  1. Call hooks at the top level of your component, never inside a condition, a loop or a nested function.
  2. Call hooks only from function components or from your own custom hooks.

React does not know hooks by name. It knows them by the order you call them in, and it expects that order to be the same on every render. A hook inside an if would shift every hook after it. The rules of hooks page shows what breaks and how to fix it. use is the one exception: it may be called inside a condition.

Your first custom hook

When two components need the same stateful logic, move it into a function whose name starts with use. That function can call any other hook. Each component that calls it gets its own state.

Both counters run the same hook, but clicking one leaves the other alone: a custom hook shares logic, not state. Add a decrement function to useCounter and a button that calls it, and both counters get it at once. More patterns (useToggle, useDebounce, useLocalStorage) are on the custom hooks page.

Which hook to reach for

  • The screen should change when a value changes: useState, or useReducer when many events update the same state.
  • You need to talk to something outside React (a timer, a subscription, a request): useEffect.
  • You need a DOM node or a value the screen does not show: useRef.
  • Many components deep in the tree need the same value: useContext.
  • A render is slow because of one calculation or because a memoized child re-renders: useMemo and useCallback. React Compiler can add this memoization for you, but writing it by hand still works and is still common.
  • Typing feels laggy because the update behind it is heavy: useTransition or useDeferredValue.

Frequently Asked Questions

What are hooks in React?

Hooks are functions whose names start with use that let a function component use React features: useState for state, useEffect for side effects, useContext for context, useRef for values that survive renders without causing one, and more.

Why were hooks added to React?

Before hooks (React 16.8), state and lifecycle methods existed only in class components, and sharing stateful logic needed mixins, higher order components or render props. Hooks put that logic in plain functions you can call from any function component and share as custom hooks.

How many hooks does React have?

React 19.2 ships about twenty built-in hooks in the react package, plus useFormStatus in react-dom. Most apps use a handful daily: useState, useEffect, useRef, useContext, useMemo and useCallback.

Can I use hooks in class components?

No. Hooks only work inside function components and other hooks. You can still render a class component next to function components, and a class can render a function component that uses hooks.

What is a custom hook?

Your own function whose name starts with use and that calls other hooks. It lets several components reuse the same stateful logic, while each component that calls it still gets its own separate state.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED