-
-
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
updates during validation on unmounted component #3418
Comments
This might be fixed in #3231 just because the underlying state manager |
@johnrom It might be that v3 version fix it, but it is not released and we should have a quick-fix for v2 time. |
Is there any eta on v3? Seems like it would fix a bunch of people's problems and enahnce existing use-cases so it's a shame that it's kind of being left in limbo for so long now. 🤔 |
Just as an FYI for anyone who has seen this bug, I've discovered that if you put the render inside an So you can replace render(<ComponentContainingFormik/>) with: await act(async () => render(<ComponentContainingFormik/>)) And the warning go away. At least in my case 😄 |
@wwahammy With your "solution" the warning is not printed, but the code is still running. I found that it is related to See: testing-library/react-testing-library#999 I hope the @gaearon could help us to understand it. |
Thanks for clarifying @artola! |
I’m on a vacation. If there’s some problem or question about React please file an issue on React issue tracker. |
I am using another workaround, might help someone: declare module 'react-dom/test-utils' {
export function act<T>(callback: () => Promise<T>): Promise<T>;
} it('should render', async () => {
const {getByTestId} = await act(async () =>
render(
<Foo />,
),
); |
Bug report
Current Behavior
When used the prop
validateOnMount={true}
the component runs the validation as required (usingvalidate
and/orvalidationSchema
produces the same behaviour), but if the component is unmounted asap it performs some state updates (dispatch
) even when an unmounted component should not.Expected behavior
Do not change state once unmounted.
Reproducible example
https://codesandbox.io/s/formik-codesandbox-template-bug-ku21b?file=/index.test.js
See console error:
Suggested solution(s)
At least in both places where it was used
useEffect
(withvalidateOnMount
as dependency) should be replaced byuseLayoutEffect
.https://github.com/formium/formik/blob/b9cc2536a1edb9f2d69c4cd20ecf4fa0f8059ade/packages/formik/src/Formik.tsx#L335
https://github.com/formium/formik/blob/b9cc2536a1edb9f2d69c4cd20ecf4fa0f8059ade/packages/formik/src/Formik.tsx#L412
With this change, it works as expected for the example provided. It is not clear to me if some other places also should change to
useLayoutEffect
, specially because of React v17 different handling of them:https://github.com/formium/formik/blob/b9cc2536a1edb9f2d69c4cd20ecf4fa0f8059ade/packages/formik/src/Formik.tsx#L165
Additional context
React v17
Your environment
The text was updated successfully, but these errors were encountered: