← Learning Hub
Intermediate

Custom Hooks

A custom hook is your own function whose name starts with use and that uses other hooks inside. It lets you reuse logic - not the screen, the behaviour - in many components.

When two components contain the same hook logic - loading data, reading localStorage, tracking window size - you can move that logic into your own hook. Custom hooks are just functions that use other hooks, named with use so React (and linters) know to apply the rules of hooks. They are React's main way of sharing behaviour between components, and many popular libraries are simply collections of custom hooks.

Example
<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.24.7/babel.min.js"></script>

    <script type="text/babel">
      const { useState, useEffect } = React;

      // Remembers a value in localStorage
      function useLocalStorage(key, initial) {
        const [value, setValue] = useState(() => {
          try {
            const saved = localStorage.getItem(key);
            return saved !== null ? JSON.parse(saved) : initial;
          } catch { return initial; }
        });
        useEffect(() => {
          try { localStorage.setItem(key, JSON.stringify(value)); } catch {}
        }, [key, value]);
        return [value, setValue];
      }

      // Tracks the window width
      function useWindowWidth() {
        const [width, setWidth] = useState(window.innerWidth);
        useEffect(() => {
          const onResize = () => setWidth(window.innerWidth);
          window.addEventListener("resize", onResize);
          return () => window.removeEventListener("resize", onResize);
        }, []);
        return width;
      }

      function App() {
        const [name, setName] = useLocalStorage("name", "");
        const width = useWindowWidth();
        return (
          <div>
            <input value={name} onChange={e => setName(e.target.value)} placeholder="Your name" />
            <p>Hello {name || "friend"}! (Press Run again - your name stays.)</p>
            <p>Window width: {width}px - {width < 600 ? "phone" : "computer"} layout</p>
          </div>
        );
      }

      ReactDOM.createRoot(document.getElementById("root")).render(<App />);
    </script>
  </body>
</html>
▶ Try it yourself

Rules of hooks

  • Call hooks only at the top level of a component or a custom hook - never inside if, loops or nested functions.
  • Call hooks only from React components or custom hooks, not from plain functions.
  • Start custom hook names with use, so React and its tools can check these rules.
Each component that calls a custom hook gets its own copy of the state inside it. Hooks share logic, not data.
Key points
  • A custom hook is a function starting with use that calls other hooks.
  • It shares logic, not state - each caller gets its own copy.
  • Follow the rules of hooks: top level only, only in components or hooks.

📝 Test yourself

1. What must a custom hook's name start with?
2. Where may you call a hook?
3. Two components use the same custom hook. Do they share state?