A React hook is called only on some renders, behind a condition, loop or early return

tx0/ts:conditional-hook-callrecommendedwarningbug

What it finds

React identifies the state of each hook by the order of the hook calls in a render, so a component or custom hook must call the same hooks in the same order every time. A hook called inside an if, a loop, a switch, a try, a ?: or after && or ||, or after an early return, is skipped on some renders, and React mixes up the state of the hooks after it or throws.

A hook is a call of a name, or of a member of React, that starts with use and a capital letter. The enclosing function must be a component or hook: a function whose name starts with a capital letter or with use and a capital letter, given by its declaration or by the variable it initializes. Calls in nested functions are judged by those functions.

From the rule's source, conditional_hook_call.rs.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:conditional-hook-call" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:conditional-hook-call] reason="..."

tx0 explain tx0/ts:conditional-hook-call shows the same in the terminal. Found a false positive? Report it.