DEV Community

Md Rashid Shabab
Md Rashid Shabab

Posted on

Understanding React Hooks: The What, Why, and How (for Real)

If you're using React, you’ve definitely run into hooks like useState, useEffect, or maybe even made your own useSomething. But what exactly is a hook? How is it different from a regular function? And why is React so strict about how they're used?


🔍 What is a Hook?

At its core, a hook is just a function that gives function components the ability to use React features like state, side effects, and context.

  • useState gives you state
  • useEffect runs side effects
  • useRef, useMemo, and others give access to React internals

But hooks are more than just function calls — they plug directly into React’s rendering engine.


🔧 How React Tracks Hooks Internally

React stores hooks in a list and matches them by call order.

On each render, it:

  1. Starts from the top of your component.
  2. Calls your hooks one by one.
  3. Matches them to internal storage by index.
  4. Hook names get erased during JavaScript minification (when your code is optimized for production). React doesn't rely on names internally — it tracks hooks at runtime based on call order.

✅ Example (safe):

function MyComponent() {
  const [count] = useState(0);     // Hook #0
  const [name] = useState('Joe');  // Hook #1
}

Enter fullscreen mode Exit fullscreen mode

Hooks always run in the same order — React matches count to slot #0 and name to slot #1 every time.

❌ Example (broken):

function MyComponent({ showName }) {
  const [count] = useState(0); // Hook #0

  if (showName) {
    const [name] = useState('Joe'); // Conditionally Hook #1? Uh oh
  }
}
Enter fullscreen mode Exit fullscreen mode

Now if showName changes, the number/order of hooks changes. React’s internal hook index gets out of sync, and your component breaks.


🔄 Custom Hooks: What Are They?

Custom hooks are just functions that call other hooks.

function useCounter(initial = 0) {
  const [count, setCount] = useState(initial);
  return {
    count,
    increment: () => setCount(c => c + 1),
    reset: () => setCount(initial)
  };
}
Enter fullscreen mode Exit fullscreen mode

✅ Naming Convention

Custom hooks must start with use so that:

  • ESLint can catch bugs (eslint-plugin-react-hooks)

  • React tools (like DevTools) recognize them

  • Your teammates (and future-you) know they're hooks

❌ What if I name it counter()?

Technically, it will still work if it calls real hooks. But React and your linter won’t treat it as a hook. That means:

  • You won’t get helpful warnings

  • Bugs (like calling hooks in conditionals) might slip by silently


🔧 Why the use Prefix Really Matters

You might wonder why React insists on the use prefix. Here's why:

  • The use prefix is mainly a developer convention and a linting hook for tools to identify hook functions statically.

  • This allows ESLint to enforce the Rules of Hooks before your code even runs, helping you avoid subtle bugs.

Top comments (0)