[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
![Image](https://private-user-images.githubusercontent.com/15115317/400210341-d6b40afd-0186-4993-b577-f29f0f6b83ac.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1Njk0OTksIm5iZiI6MTczOTU2OTE5OSwicGF0aCI6Ii8xNTExNTMxNy80MDAyMTAzNDEtZDZiNDBhZmQtMDE4Ni00OTkzLWI1NzctZjI5ZjBmNmI4M2FjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDIxMzk1OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTJiOTM0YzY3YTdhZWE5YTg5NDAyNDEwZDQ2OTgxY2FhNDUxNGRhNzYwODU5OTMzOTJmZjI2ODQyNTM5YTM2YjYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.6R5uuHYuD1wQ356DAIeb-wx2LRmB2xtb6Z3z-dcNVUA)
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: