-
-
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
Should react/no-did-mount-set-state trigger on ES2017 async componentDidMount? #1110
Comments
Duplicate of/related to #56 |
Yes, that seems reasonable. |
I believe that unfortunately, we can't be sure that a given statement is after an await statement: async componentDidMount() {
try {
if (condition) {
await someAsyncOperation();
}
this.setState({ ... }); // here
} catch (e) {
this.setState({ ... });
}
} Depending on the condition, the line marked We can verify this by executing this snippet: async function f(condition) {
if (condition) {
await 1;
}
console.log("after condition");
}
// will log "after call", then "after condition", proving that the execution was asynchronous
f(true);
console.log("after call");
// will log "after condition", then "after call", proving that the execution was synchronous
f(false);
console.log("after call"); |
No, the spec for await means that the line after the |
I haven't written anything about a "returned" value. I'm not saying that |
ahh, i see what you mean. sure, async functions execute synchronously until the first |
Exactly. Because of that we can't always statically determine if a setState() is called asynchronously or not. I don't see how we could implement the suggested rule except for the simple cases (e.g. a |
You're right; we'd have to treat a setState as sync unless all possible code paths included at least one |
What about an option to never trigger when componentDidMount is async ? |
I would also like such an option. When componentDidMount is async it's rarely the case that the user would try to do a setState before having waited for some promise. The other alternative for me is to simply disable this rule, as it's annoying for the devs using async/await |
Rarely isn’t the same as never; it’s better to overwarn than underwarn here imo. |
@ljharb not saying to make it a default, just an option to opt-out for overwarn |
I’d prefer to fix the default so it properly warns. |
There's no news; the state of this issue is that it is still awaiting a PR. |
The rule
react/no-did-mount-set-state
triggers error on async componentDidMount function for a deferredsetState
. Basically,this.setState()
is being called here after an async operation has been completed. It is not re-rendering straight after mounting, but only after some data has been retrieved from a server. Does it make sense to error on this?The text was updated successfully, but these errors were encountered: