What Makes a Custom Hook
What you'll learn: The simple definition and rules that turn an ordinary function into a custom hook in React.
The Core Definition
A custom hook is just a JavaScript function with two distinguishing characteristics:
- Its name must start with "use" (like
useEffectoruseState) - It can call other hooks inside its body
That's it. There's no special API, no class to extend, no magic syntax. If your function follows these two rules, React treats it as a hook and applies all the hook rules to it.
Why the "use" Prefix Matters
The "use" prefix isn't just convention—it's a signal to React's linter and developer tools. When a function starts with "use", React knows:
- It must follow the Rules of Hooks (no conditional calls, only at top level)
- It can safely call
useState,useEffect,useContext, etc. - The linter should enforce dependency array rules inside it
Without the "use" prefix, React can't distinguish your function from a regular utility function.
The Analogy
Think of custom hooks like specialized tools in a workshop. The built-in hooks (useState, useEffect) are your basic hammer and screwdriver. A custom hook is when you combine those basic tools into a specialized tool—like a drill with multiple bits. It's still made from basic parts, but now it's packaged for a specific job.
Example Definition (Structure Only)
function useWindowWidth() {
// Can call useState, useEffect, etc. here
// Returns any value you want
}
This is a valid custom hook because it starts with "use" and could call other hooks internally.
Key Takeaway: A custom hook is any function whose name starts with "use" and that calls other React hooks—no special syntax required, just naming and composition.