Home / Learn React / Custom Hooks

Custom Hooks

A custom hook is your own function whose name starts with use and that calls other hooks. It lets you share logic (not the UI) between components.

In shortMove repeated hook logic into a function named useSomething. Each component that calls it gets its own separate state.

Theory in plain words

Extract logic into a hookComponent Arepeated logicComponent Brepeated logicuseThing()one shared functionA and B just calluseThing()
Extract logic into a hook

What it is

A custom hook is a normal function that uses built in hooks inside it. React treats any function named useX as a hook and checks the rules of hooks for it.

Why we use it

If three components have the same useState + useEffect code, copying it is a bug waiting to happen. A custom hook gives that logic a name, one home and one place to fix.

How it works

Calling a custom hook just runs its inner hooks as if you wrote them inside your component. Two components that call the same hook do not share state: each call makes its own.

Where it is used

  • useLocalStorage, useDebounce, useToggle, useMediaQuery
  • Data fetching hooks such as useFetch
  • Form handling hooks
  • Library APIs (React Router useNavigate, TanStack useQuery)

useToggle

A tiny hookTry it Yourself

useLocalStorage

State that survives a reloadTry it Yourself

In this sandbox localStorage may be blocked. The code falls back safely. In your own project it works as expected.

useDebounce

Wait until typing stopsTry it Yourself

useFetch

Loading, error and data in one hookTry it Yourself
Careful:Custom hooks share logic, not state. If two components must see the same value, lift the state up or use context.

Quick quiz

1. Do two components that call the same custom hook share state?

2. A custom hook may call...

3. What is a good use for a custom hook?

Common questions

Do two components that call the same custom hook share state?

No, each call has its own state. Hooks share logic, not state.

What is a good use for a custom hook?

Repeated effect and state logic such as useDebounce. Extract repeated logic.