You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
S6442 "React's useState used directly in render function"
Reproduce
False positive on some edge cases where a derived state is intended.
Explanation
As pointed out by a user, there are some corner cases where you might legitimately want to call the state setter in the render, although inside a condition to avoid the infinite loop.
React discourages this pattern ("This pattern can be hard to understand and is usually best avoided"), but I guess it is a false positive nonetheless.
Suggestion
I suggest we ignore cases where the state setter is inside a conditional.
For example:
The title of the rule says the opposite of what we want:
- React's useState hook should only be used in the render function or body of a component+ React's useState hook should not be used directly in the render function or body of a component
While we are there, there are also extra semicolons in the code examples:
function ShowLanguage() {
...
-};+}
The text was updated successfully, but these errors were encountered:
Explanation
As pointed out by a user, there are some corner cases where you might legitimately want to call the state setter in the render, although inside a condition to avoid the infinite loop.
React discourages this pattern ("This pattern can be hard to understand and is usually best avoided"), but I guess it is a false positive nonetheless.
Suggestion
I suggest we ignore cases where the state setter is inside a conditional.
For example:
Perhaps we can go all the way and require an explicit return statement since this is what React is fundamentally doing internally.
Corrections
The title of the rule says the opposite of what we want:
While we are there, there are also extra semicolons in the code examples:
The text was updated successfully, but these errors were encountered: