-
-
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warning: Can't perform a React state update on an unmounted component #2430
Comments
It looks like the warning is occurring because by the time you call signIn, isAuthenticated is going to start returning true, so you're going to start rendering |
@johnrom I maybe approaching this wrong but how would I handle and error coming back from the server then? Seems like my current design needs to be rethought. |
I'm not sure the signature of your sign In method, but if it returns a condition that results in Formik continuing to render, you can continue to use your callback with that check. signIn().then(result => {
if (!result.isAuthenticated) {
setSubmitting(false);
// etc
}
}); |
The warning maybe possible to work around in this particular example, but within a more complex application setup, I think it's reasonable to do:
Maybe this issue is specific to class components and doesn't happen with functional components. Maybe it's wrong to do If one of the impacts of Or continuing the example above, imagine that |
I wonder if it would be easier for Formik to check whether it is still mounted within setSubmitting. There should technically be no issue here. The warning is mostly for situations where code continues depending on unmounted components. Once this callback is completed, references to the component should be garbage collected properly and thus there shouldn't be a memory leak - but I don't have a repro in front of me to confirm that. If that's the case, there's no problem calling setSubmitting after the component unmounts and we should make the check within Formik if possible. |
@johnrom I think that's a good suggestion. Here's the repo that has the issue: https://github.com/vicesoftware/react-redux-hooks-boilerplate It's a boilerplate so it's not over complex and weighted down with business logic. It's well documented and quick and easy to run. It's the signin component that has the issue. |
It's not ideal, but you can check if the future state of redux is const MyForm = () => {
const store = useStore();
return <Formik
onSubmit={() => signIn.then(() => {
if (!store.getState().authentication.isAuthenticated) {
resetForm();
setSubmitting(false);
}
})}
/>;
} I'm still on the fence about whether to make this check in Formik. |
My solution is just to check if
|
Is that the proper solution or just a workaround around a formik bug? I.e. should this actually be closed? |
Needs further evaluation |
I encountered this issue when a field error was returned from my server because of how I was rendering my loading spinner. My code..
Note* I was not having this issue with a successful login. |
I solved my issue with the following solution, it worked for me. Below is your possible solution.
|
I encountered this bug in a different scenario where I couldn't simply check a store state. FWIW, I think it's reasonable for Formik to handle this, since the usage of That said, I was able to fix this in my code using this method of using hooks to see if my wrapper component was mounted before calling Ideally this check would be made within |
馃悰 Bug report
Current Behavior
I'm getting this error
For this component
Expected behavior
To not get a warning
Reproducible example
https://github.com/vicesoftware/react-redux-hooks-boilerplate/tree/formik-warning-demo
cd webapp
npm install
npm start
http://localhost:3000/signin
ryan@vicesoftware.com
andpassword
see warning
Suggested solution(s)
Additional context
Your environment
The text was updated successfully, but these errors were encountered: