no-useless-custom-hooks
Full Name in eslint-plugin-react-hooks-extra
Full Name in @eslint-react/eslint-plugin
Features
🔍
Presets
recommended
recommended-typescript
recommended-type-checked
What it does
Enforces custom Hooks to use at least one other Hook inside.
If your function doesn’t call any Hooks, avoid the use
prefix. Instead, write it as a regular function without the use
prefix. For example, useSorted
below doesn’t call Hooks, so call it getSorted
instead:
This ensures that your code can call this regular function anywhere, including conditions:
You should give use
prefix to a function (and thus make it a Hook) if it uses at least one Hook inside of it:
Technically, this isn’t enforced by React. In principle, you could make a Hook that doesn’t call other Hooks. This is often confusing and limiting so it’s best to avoid that pattern. However, there may be rare cases where it is helpful. For example, maybe your function doesn’t use any Hooks right now, but you plan to add some Hook calls to it in the future. Then it makes sense to name it with the use
prefix:
Then components won’t be able to call it conditionally. This will become important when you actually add Hook calls inside. If you don’t plan to use Hooks inside it (now or later), don’t make it a Hook.