TSLint Rules of Hooks
A TSLint rule that enforces the Rules of Hooks for React hooks.
The rule is based on an ESLint plugin for react hooks.
- detects using React hooks inside potentially-conditional branches:
- if statements
- short-circuit conditional expressions (
- ternary expressions
- loops (
do ... while)
- functions that themselves are not custom hooks or components
- detects using React hooks in spite of an early return
- support for detecting hooks from namespaces other than
First, install the rule:
npm install tslint-react-hooks --save-dev
Then, enable the rule by modifying
"extends":// your other plugins..."tslint-react-hooks""rules":// your other rules..."react-hooks-nesting": "error"
To use report rule violations as warnings intead of errors, set it to
While the rule works fine out-of-the-box, it can be customized. To specify options, use the
following syntax when modifying
"extends":// your other plugins..."tslint-react-hooks""rules":// your other rules..."react-hooks-nesting":"severity": "error" // "error", "warning", "default" or "off""options":// options go here
"detect-hooks-from-non-react-namespace"- when set to
true, violations will be also reported hooks accessed from sources other than the
MyHooks.useHookwill be treated as a hook).
By default, only direct calls (e.g.
useHook) or calls from
React.useState) are treated as hooks.
Have an idea for an option? Create a new issue.
For some arrow functions/function expressions, the rule has no way to determine whether those are a component, a hook, both of which could contain hook calls, or a regular function that should not contain hook calls.
The workaround in those cases is to use a named function expression:
Naming the function like a component (in PascalCase) unambiguously lets the rule treat the function as a component.
False positives and not-covered cases
There are some cases that seem hard to analyze and may result in false positives or false negatives.
In such cases, disable the rule for a specific line using the following comment:
Looping over static arrays
The rule may report false positives, for example in:
useEffect hook will be called unconditionally and the call-order will be the same between
Using renamed hooks (that do not start with use)
The rule only treats functions that start with use as hooks. Therefore, renaming the hook will result in avoiding the rule:
Unconditional nesting, for example:
is treated as conditional nesting. It seems hard to verify if the condition is in fact a constant, therefore such a situation will always result in a rule violation.
In situations where such an
if statement was used to create an additional block scope, use the
block scope directly:
After pulling the repository, make sure to run
The source code for the rule is located in the
For more information about the developing custom TSLint rules, take a look at TSLint's documentation.
Testing the rule
npm run test
to compile the rule and run automatic TSLint tests.
They are located in the
The author of this rule is Grzegorz Rozdzialik.