[Bug]: hook-use-state
reports when setter is not destructured
#3871
Labels
hook-use-state
reports when setter is not destructured
#3871
Is there an existing issue for this?
Description Overview
Sometimes it's necessary to initialize value but never update it. To get a
ref
-like behavior with more convinient API.The following code:
Produces a warning when
react/hook-use-state
rule is enabled:Expected Behavior
I acknowledge that this is kind of controvercial because currently it also protects from forgetting a setter. Thus I suggest this behavior to be configurable. Furthermore, config can be done in 2 ways:
allowMissingSetter
-boolean
(defaultfalse
)setterIgnorePattern
-RegExp
that will allow to opt-out of setter check. This is similar to ESLint'sno-unused-vars
varsIgnorePattern
config.I prefer solution 2 as it's not only more flexible and consistent but also protects from missing setter. The code above could be rewritten as:
This way it explicitly means: I don't need a setter.
eslint-plugin-react version
7.37.3
eslint version
8.57.0
node version
20
The text was updated successfully, but these errors were encountered: