Skip to content
HooksChapter 21 / 37Intermediate4 min read

Custom Hooks

Learn how to pull repeated stateful logic into reusable React custom hooks, with naming rules and real examples for beginners.

On this page

1. What are custom hooks?

When two or more components share the same logic, like checking online status or tracking the window size, you can pull that logic out into a custom hook.

Every component that calls the hook gets its own separate state. A hook shares logic, not state.

2. Why do they matter?

Without a custom hookWith a custom hook
The same useState and useEffect in every componentThe logic lives in one hook, one line everywhere
A bug fix has to be made in three placesOne fix, and all components are correct
Components get long and confusingComponents focus only on UI
Logic is hard to testYou can test the hook on its own

3. Where are they used?

Hook nameWhat it does
useOnlineStatusTells you whether the user is online or offline
useLocalStorageSyncs state with localStorage
useDebounceUpdates a value after typing stops
useWindowSizeThe width and height of the screen
useAuthGets the current user from Context

4. How do they work?

The process of extracting logic
Notice the same hook logic in two components
Move it into a function that starts with use
Return the values that are needed
Call the hook in your components
Each call gets its own separate state

The Rules of Hooks apply to custom hooks too: call them at the top level, not inside loops or conditions.

5. Code example

useOnlineStatus

tsx
import { useEffect, useState } from "react";

export function useOnlineStatus() {
  const [isOnline, setIsOnline] = useState(true);

  useEffect(() => {
    const update = () => setIsOnline(navigator.onLine);
    update();
    window.addEventListener("online", update);
    window.addEventListener("offline", update);
    return () => {
      window.removeEventListener("online", update);
      window.removeEventListener("offline", update);
    };
  }, []);

  return isOnline;
}

Using it in two components

tsx
import { useOnlineStatus } from "./use-online-status";

export function StatusBar() {
  const isOnline = useOnlineStatus();
  return <p>{isOnline ? "Online" : "Offline"}</p>;
}

export function SaveButton() {
  const isOnline = useOnlineStatus();
  return <button disabled={!isOnline}>Save</button>;
}

useDebounce

tsx
import { useEffect, useState } from "react";

export function useDebounce<T>(value: T, delay = 400) {
  const [debounced, setDebounced] = useState(value);

  useEffect(() => {
    const id = setTimeout(() => setDebounced(value), delay);
    return () => clearTimeout(id);
  }, [value, delay]);

  return debounced;
}

How does the code work?

PartMeaning
useOnlineStatusThe use prefix tells React and the linter that this is a hook
useEffect cleanupRemoves the listeners when the component is removed
return isOnlineThe hook returns only the value that is needed
Two componentsEach has its own separate isOnline state
useDebounce<T>A generic hook that works with a value of any type

6. Common mistakes

Mistake: Not starting the function name with use

Why? The linter cannot check the Rules of Hooks, and using hooks inside it looks wrong.

Better: Name every function that calls hooks useSomething.

Mistake: Turning every function into a hook

Why? If no hook is used inside, it is just a normal helper function.

Better: Write a simple function for logic without hooks, like formatPrice.

Mistake: Thinking a custom hook shares state

Why? Each call has its own state, so a change in one component is not visible in another.

Better: If you need to share state, use lifting state up or Context.

Interview tip

A good custom hook does one thing, and its name tells you what it does. Avoid generic lifecycle hooks like useMount, because they go against React's model.

7. What to learn before and after

Final quick revision

PointDetail
What it isA function starting with use that calls hooks
What is sharedLogic, not state
RulesThe Rules of Hooks apply here too
Return valueWhatever is needed: a value, an array or an object
BenefitReusable, testable, clean components