# Custom Hooks

> How to extract and share stateful logic between components without sharing the state itself.

Subject: React | Lesson 23 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/custom-hooks
Previous: [The Context Performance Trap](https://codewithanimation.in/react/context-performance-trap.md) | Next: [Error Boundaries](https://codewithanimation.in/react/error-boundaries.md)

## What is a Custom Hook?

A custom Hook is just a regular JavaScript function whose name starts with `use` (like `useWindowSize` or `useFetch`).

Unlike normal helper functions, a custom Hook is allowed to call other React Hooks (like `useState` and `useEffect`) inside it.

## Reusing Stateful Logic

If you build a component that tracks the window's width (using `useState` and a `resize` event listener in `useEffect`), and later you need that exact same logic in a completely different component, you shouldn't copy-paste the code.

Instead, you extract that logic into a `useWindowSize` hook. Now both components can just call `const width = useWindowSize();`.

## How to build one

**1. Identify duplicated logic:** Look for `useState` and `useEffect` patterns repeated across components.

**2. Extract it:** Create a function named `useSomething` and move the logic inside it.

**3. Return the data:** Return the state variable (or anything else the components need) from your hook.

**4. Use it:** Call your new hook from your components just like a built-in React hook.

## Common mistake: They Share Logic, Not State

This is the most common misunderstanding about custom hooks.

If Component A and Component B both call `useFormInput()`, **they do not share the input state.**

Custom hooks are a mechanism to reuse *behavior* (the logic of how state works), not to share data. Every time you call a hook, it creates a completely independent instance of state for that specific component. If you want to share data, you need Context or a global store.

## Tip: The 'use' prefix is not optional

You must name your custom hooks starting with `use` (e.g. `useOnlineStatus`). This is how React's linter knows that the function is a Hook and must obey the Rules of Hooks (like not being called inside loops or conditions).

## Try it: Logic Extraction

Click 'Extract Logic' to watch how duplicated code in two separate components is pulled out and merged into a single reusable custom hook, leaving behind clean components.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/custom-hooks

## Check yourself

### Question 1

If Component A and Component B both call `const count = useCounter()`, what happens to the state?

- A. They share the exact same `count` variable. If A updates it, B updates.
- B. They get completely independent copies of the state. If A updates it, B is unaffected.
- C. React throws an error because hooks can only be called once.

Correct answer: B

A custom hook is just a function. Every time a component calls it, it runs `useState` again, creating an isolated piece of state for that specific component.

- Why not A: Custom hooks share logic, not state.
- Why not C: Hooks can be called in as many components as you want.

### Question 2

Why must a custom hook's name start with 'use'?

- A. It's just a common convention, but not strictly required.
- B. It tells the React compiler to optimize the function differently.
- C. It tells the React linter that this function contains Hooks and must obey the Rules of Hooks.

Correct answer: C

Without the `use` prefix, the linter wouldn't know to check if you accidentally put a `useState` inside an `if` statement within your custom function.

- Why not A: The React linter relies on this prefix to enforce the Rules of Hooks.
- Why not B: React doesn't optimize it differently; it's a linter requirement.

### Question 3

When should you create a custom hook?

- A. When you want to share data between completely unrelated components in different parts of the tree.
- B. When you find yourself duplicating `useState` and `useEffect` logic across multiple components.
- C. When a component's JSX gets too long and needs to be split up.

Correct answer: B

Custom hooks are specifically for extracting and reusing stateful logic.

- Why not A: That's what Context or global state managers are for.
- Why not C: You would create smaller child components for that, not custom hooks.

## Remember this

- Custom hooks extract stateful logic to prevent code duplication.
- Their names MUST start with 'use'.
- They are allowed to call other hooks (like `useState` and `useEffect`).
- They share LOGIC, not STATE. Every call creates isolated state.
