Skip to content
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

Latest alpha - max update depth exceeded #2968

Open
kelly-tock opened this issue Dec 16, 2020 · 20 comments
Open

Latest alpha - max update depth exceeded #2968

kelly-tock opened this issue Dec 16, 2020 · 20 comments

Comments

@kelly-tock
Copy link

Bug report

When using a custom input on latest alpha I get a max update exceeded. It looks like the useReducer in my example in formik is being run and re-initializing the initial values.

in formik 2 this is not happing.

Current Behavior

type 1 character into the field, app will crash

Expected behavior

Reproducible example

https://github.com/kelly-tock/formik-alpha-repro

Suggested solution(s)

Additional context

Your environment

Software Version(s)
Formik "formik": "^3.0.0-next.8",
React "react": "^17.0.1",
TypeScript "typescript": "^4.0.3",
Browser latest chrome
npm/Yarn 1.22.5
Operating System os x 10.15.7
@kelly-tock
Copy link
Author

if you install formik@latest, this example repo will work.

@johnrom
Copy link
Collaborator

johnrom commented Dec 16, 2020

@kelly-tock reproductions should be codesandboxes. codesandboxes will allow a maintainer to reproduce immediately without needing access to a dev environment

@kelly-tock
Copy link
Author

ah, ok, let me do that

@kelly-tock
Copy link
Author

https://codesandbox.io/s/formik-codesandbox-template-forked-mc4mv?file=/index.js

toggle formik between latest alpha and latest 2.x to see the difference.

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Jan 16, 2021
@kelly-tock
Copy link
Author

Bump

@github-actions github-actions bot removed the stale label Jan 17, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Feb 17, 2021
@kelly-tock
Copy link
Author

Bump

@johnrom johnrom removed the stale label Feb 17, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Mar 20, 2021
@kelly-tock
Copy link
Author

Bump

@github-actions github-actions bot removed the stale label Mar 21, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Apr 21, 2021
@johnrom johnrom removed the stale label Apr 21, 2021
@kelly-tock
Copy link
Author

Bump

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label May 22, 2021
@kelly-tock
Copy link
Author

Bump

@github-actions github-actions bot removed the stale label May 23, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Jun 23, 2021
@johnrom johnrom removed the stale label Jun 23, 2021
@kelly-tock
Copy link
Author

Bump

@github-actions
Copy link
Contributor

github-actions bot commented Aug 7, 2021

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Aug 7, 2021
@kelly-tock
Copy link
Author

bump

@johnrom
Copy link
Collaborator

johnrom commented Aug 11, 2021

I don't know how react-number-format works or why you need it specifically, but it seems like the exact functionality it's used for is covered by the format prop in v3.

With functional components, a lot of the default optimizations from class components have been taken away, so in v3 we brought this functionality back by optimizing using a layout effect + useSubscription (see https://www.npmjs.com/package/use-subscription). What this means is that the next update to child components doesn't occur until the render after Formik's internal state updates, and all code which uses the values in Formik must use the subscription in order to see the actual value.

Your react-number-format input is not treating Formik as the single source of truth for its own value, and thus it cannot work with Formik's alpha (it's tracking its own internal value, and then when the next render comes and the new prop is equal to the old value, it triggers another update, then triggers, another update to formik, then the new old previous value triggers another update in an infinite loop).

This is probably a good reason to avoid going ahead with this alpha. I'd have to find a way to work around this issue, but the only workaround I know of is to use unstable_BatchedUpdates outside of an effect which is.. well.. unstable and isn't a perfect integration with alternate renderers. However, I'm pretty sure this is what redux does so it's not without precedent. In theory, every component which consumes Formik's state should use its API as the single source of truth, but in reality that would put an unreasonable burden on developers to make changes to support this.

Concurrent mode + useMutableSource might help since if I understand correctly it should cancel the render before it is committed and the effects run, skipping right to the 2nd render where both sources match.

@github-actions github-actions bot removed the stale label Aug 12, 2021
@johnrom
Copy link
Collaborator

johnrom commented Aug 12, 2021

cc: @jaredpalmer

I pushed a fix to my PR #3231 to use unstable_batchedUpdates in both React-Dom and React Native to fix this issue:

https://codesandbox.io/s/formik-codesandbox-template-forked-jy3k7?file=/index.js

React Native isn't actually working since I don't know how to configure tsdx to create an alternate index.native.js entrypoint. But if we can create one, it would work.

Note that batched updates won't work in alternate renderers, and alternate renderers need to use the entrypoint ./index.alternate.js similar to the way Redux does. I also exported setBatch to support custom batch fns in alternate renderers.

@johnrom johnrom mentioned this issue Aug 26, 2021
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants