-
Notifications
You must be signed in to change notification settings - Fork 10
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
Detect use of useState inside useEffect #16
Comments
Thanks for posting an issue 🙂 Are you sure that function MyComponent() {
React.useEffect(() => {
React.useState(() => {
})
})
} results in a rightful rule violation: Could you share a snippet of this case that does not result in a rule violation? 🙂 |
Hi, I just found that, in some case likes: const [currentRenderCounter, setRenderCounter] = useState(0);
useEffect(() => {
if (shouldRender) {
setRenderCounter(counter => counter + 1);
console.log(currentRenderCounter);
... Will cause infinite loop of rendering. But in some case, it's fine to use state in The safe way is to useRef: const renderCounter = useRef(0);
useEffect(() => {
if (shouldRender) {
renderCounter.current += 1;
console.log(renderCounter.current);
... |
I am not convinced that this is a responsibility of the linter to report such behavior 🙂 Statically analyzing the code would not result in enough information to determine that Moreover, in some cases, the code snippet you provided is the wanted behavior, as in some cases All in all, I have no easy idea on how to implement such verification, therefore I am closing this issue. If you, or anyone else, has an implementation idea, feel free to create a PR 🙂 |
This will cause lots of rerender, without warning.
The text was updated successfully, but these errors were encountered: