ensure-custom-hooks-using-other-hooks
Rule category
Correctness.
What it does
Warns when custom Hooks that don’t use other Hooks.
Why is this good?
Custom Hooks may call other Hooks (that’s their whole purpose). If a custom Hook is not calling other Hooks, it might be a sign that it’s unnecessary or incorrectly implemented. This rule helps you catch those cases.
Examples
Failing
import React from 'react';
function function useClassnames(obj: Record<string, boolean>): string
useClassnames(obj: Record<string, boolean>
obj: type Record<K extends keyof any, T> = { [P in K]: T; }
Construct a type with a set of properties K of type TRecord<string, boolean>) {Custom Hooks should use other Hooks. var function (local var) k: any
k, function (local var) cls: string
cls = "";
for (function (local var) k: any
k in obj: Record<string, boolean>
obj) {
if (obj: Record<string, boolean>
obj[function (local var) k: string
k]) {
function (local var) cls: string
cls && (function (local var) cls: string
cls += " ");
function (local var) cls: string
cls += function (local var) k: string
k;
}
}
return function (local var) cls: string
cls;
};
Passing
import React from 'react';
function function useCounter(): {
count: number;
increment: () => void;
}
useCounter() {
const [const count: number
count, const setCount: React.Dispatch<React.SetStateAction<number>>
setCount] = React.function React.useState<number>(initialState: number | (() => number)): [number, React.Dispatch<React.SetStateAction<number>>] (+1 overload)
Returns a stateful value, and a function to update it.useState(0);
const const increment: () => void
increment = () => const setCount: (value: React.SetStateAction<number>) => void
setCount(c: number
c => c: number
c + 1);
return { count: number
count, increment: () => void
increment };
}