-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[New Rule] prefer-use-state-lazy-initialization #3579
base: master
Are you sure you want to change the base?
[New Rule] prefer-use-state-lazy-initialization #3579
Conversation
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 for that repo says its for enforcing the Rules 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 it enforces the rules - it doesn't say they're only 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 a PR over there to see what they say. |
380e32c
to
51d342b
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