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.
Theory in plain words
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
useLocalStorage
In this sandbox localStorage may be blocked. The code falls back safely. In your own project it works as expected.
useDebounce
useFetch
Quick quiz
1. Do two components that call the same custom hook share state?
Hooks share logic, not state.
2. A custom hook may call...
It is a function that uses other hooks.
3. What is a good use for a custom hook?
Extract repeated logic.
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.