-
Notifications
You must be signed in to change notification settings - Fork 46.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
Bug: Bad state updates with Promise.resolve and useReducer #24650
Comments
See also #24649. I've actually discovered it because of |
I think the expected behavior should be no error at all. This is an interesting scenario because if you replace the dependency array with This leads me to think useEffect is seeing changes in The question is, why does the issue happen when the dependency array has an object but doesn't happen with |
@ritwaldev
I think because a literal value "has more chance" to look equal, on useEffect perspective, even if updates are applied out of order on the other hand |
Do you think this issue could be related to the following one? |
@antonvialibri1 Yes i found this issue while using formik submit in useEffect. |
@gffuma Yeah, hopefully Formik maintainers will address this issue soon. |
@antonvialibri1 in my opinion this is an issue with React, or at least, this behaviour should be documented. |
@gffuma My assumption is that it's related to Automatic Batching which was introduced with React 18. I tried debugging Formik's code to understand what happens. If you look at my issue here you will that for this Formik code: works like this with React 17:
Whereas, with React 18:
It's a tiny implementation detail, but I think it potentially affect other use cases and therefore should be treated as a React 18 breaking change, even though they mention that upgrading from version 17 to version 18 should happen seamlessly. The Formik team could address the issue and come up with a workaround, but I agree with you, the main issue is with React 18. |
@jaredpalmer this is a show stopper to me, any chance for a comment or advice? |
@antonvialibri1 how did you solve your problem from August then? |
Hi @matths, in my case the issue was due to a const SomeComponent = (props) => {
const [field, meta, helpers] = useField(props);
const [value, setValue] = useState(field.value || 'initial');
useEffect(
() => {
if (field.value !== value) {
helpers.setValue(value);
}
},
// For React 18, I needed to remove `field.value` and `helpers` from the deps array
// as an infinite loop was caused by `helpers.setValue(value);` when using React 18.
// I've created an issue on the Formik repo on GitHub that's worth tracking: https://github.com/jaredpalmer/formik/issues/3602
//
// This might actually be a bug of `useReducer` that was introduced in React 18: https://github.com/facebook/react/issues/24650
//
// eslint-disable-next-line react-hooks/exhaustive-deps
[value /*, field.value, helpers */]
);
// ...
} The temporary solution was to comment out I hope this issue is addressed by Formik's maintaners at some point. To me it feels like a React breaking change though. Hope this helps! |
Hi @antonvialibri1, thank you for your quick reply, but I don't get it. const SelectFormField = ({options}) => {
const [field] = useField(props);
const {setFieldValue} = useFormikContext();
useEffect(() => {
if (options.length === 1 && field.value !== options[0]) {
console.log("DEBUG: ", field.value, options[0]);
setFieldValue(field.name, options[0]);
}
}, [options, field.name, field.value, setFieldValue]);
return (
<Select
{...field}
options={options}
/>
);
}; I am able to wrap the Leaving out And it doesn't matter if I use setValue from the useField hook or setFieldValue from the useFormikContext hook. |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
I'll bump it, given the fact that we ran into this beginning last month and are on latest versions. |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
still facing this issue with react 18.2.0 |
React version: 18.1.0
Steps To Reproduce
Code Example
I think is the same bug of #24625
But i fill this issue because this scenario breaks a lot of library out of there i found this bugs using https://github.com/jaredpalmer/formik
If you type fast or you slowdown your cpu using dev tools you see the page freeze and you will see:
Warning: Maximum update depth exceeded.
The current behavior
Not seeing Warning: Maximum update depth exceeded and freeze
The expected behavior
See Warning: Maximum update depth exceeded and freeze
The text was updated successfully, but these errors were encountered: