Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
[New Rule] prefer-use-state-lazy-initialization#3579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
[New Rule] prefer-use-state-lazy-initialization#3579
Uh oh!
There was an error while loading.Please reload this page.
Conversation
codecovbot commentedMay 16, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov Report
@@ Coverage Diff @@## master #3579 +/- ##==========================================- Coverage 97.62% 97.51% -0.12%========================================== Files 132 133 +1 Lines 9295 9320 +25 Branches 3400 3414 +14 ==========================================+ Hits 9074 9088 +14- Misses 221 232 +11
|
I think that rules that are specific to hooks belong in |
The description forthat repo says its for enforcing theRules of Hooks. There are 2 rules of hooks, and thus only 2 eslint rules in that repo. This rule is for finding wasteful function calls. It's oriented around the useState hook, but hooks are now a core part of React. I think not including rules related to them would leave out a huge part of React. |
Then it should be named "eslint-plugin-react-rules-of-hooks` or something :-) The readme there says itenforces the rules - it doesn't say they'reonly for that. I think it's worth trying to PR it into there before trying here. |
The more I think about it, it does kind of make sense, especially since this is something their docs talk about. I created aPR over there to see what they say. |
380e32c
to51d342b
Compare
Adds a new rule to detect function calls inside useState and recommends that an initializer function be used instead. For example, this code:
const [value, setValue] = useState(generateTodos());
Would trigger the rule into recommending this instead:
const [value, setValue] = useState(() => generateTodos());
More info:React docs on avoiding recreating initial state