- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
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.
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:
Example (safe):
function MyComponent() {
const [count] = useState(0); // Hook #0
const [name] = useState('Joe'); // Hook #1
}
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
}
}
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)
};
}
Naming Convention
Custom hooks must start with use so that:
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 might wonder why React insists on the use prefix. Here's why:
? 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:
- Starts from the top of your component.
- Calls your hooks one by one.
- Matches them to internal storage by index.
- 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.
function MyComponent() {
const [count] = useState(0); // Hook #0
const [name] = useState('Joe'); // Hook #1
}
Hooks always run in the same order — React matches count to slot #0 and name to slot #1 every time.
function MyComponent({ showName }) {
const [count] = useState(0); // Hook #0
if (showName) {
const [name] = useState('Joe'); // Conditionally Hook #1? Uh oh
}
}
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)
};
}
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
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
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.