Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Custom Hooks in React

While working with React, you might notice that sometimes we write the same logic in multiple components. For example, imagine we have a counter in both the Home and Profile components. Both need useState() and an incre

While working with React, you might notice that sometimes we write the same logic in multiple components.

For example, imagine we have a counter in both the Home and Profile components. Both need useState() and an increment function.

Instead of writing the same code again and again, React allows us to create something called Custom Hooks.

Let's understand how they work.

1. What is a Custom Hook?

A Custom Hook is simply a JavaScript function that lets us reuse logic between different components.

It usually uses built-in React Hooks like useState, useEffect, and others.

One important thing: the name of a custom hook must start with use.

For example:

  • useCounter()
  • useFetch()
  • useForm()

2. Why do we need Custom Hooks?

Let's say we have two components that need a counter.

Without a custom hook, we would write the same logic in both components.

import { useState } from 'react';

function Home() {
    const [count, setCount] = useState(0);

    function increment() {
        setCount(count + 1);
    }

    return (
        <button onClick={increment}>
            Home: {count}
        </button>
    );
}

We would write almost the same code again in the Profile component.

It works, but what if we need this counter logic in five or six components?

That's where custom hooks become useful.

3. Creating Our First Custom Hook

Let's take the counter logic and move it into a separate file.

useCounter.js

import { useState } from 'react';

export function useCounter() {
    const [count, setCount] = useState(0);

    function increment() {
        setCount(count + 1);
    }

    return { count, increment };
}

That's it! We have created our first custom hook.

Now let's see how to use it.

4. Using the Custom Hook

We can import the same hook into different components.

Home.jsx

import { useCounter } from './useCounter';

function Home() {
    const { count, increment } = useCounter();

    return (
        <button onClick={increment}>
            Home: {count}
        </button>
    );
}

Profile.jsx

import { useCounter } from './useCounter';

function Profile() {
    const { count, increment } = useCounter();

    return (
        <button onClick={increment}>
            Profile: {count}
        </button>
    );
}

Now both components use the same counter logic without repeating the code.

5. Do Custom Hooks Share State?

This is something I initially found a little confusing.

Even though both components use the same useCounter() hook, their counter values are independent.

For example:

  • Home counter = 5
  • Profile counter = 2

Updating the Home counter won't change the Profile counter.

Why?

Because every time a component calls a custom hook, React manages a separate state for that component.

So, remember:

Custom Hooks share logic, not state.

If you want to share the same state between components, you can use Context API or other state management solutions.

6. When Should We Use Custom Hooks?

Custom hooks are useful when:

  • We repeat the same stateful logic in multiple components.
  • We want to separate logic from UI.
  • We want to make components cleaner and easier to maintain.
  • We want to reuse logic without duplicating code.

Conclusion

At first, custom hooks might look like just another function. But they are really useful when our React application starts growing.

Instead of repeating the same logic everywhere, we can write it once and reuse it wherever we need.

That's the main idea behind Custom Hooks!

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.