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

Fix validation on stale values #3947

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Jestermaxrko
Copy link

Fixes issue - #2083

The cause of the issue is in this line

const state = stateRef.current;

beause of assigning value from stateRef.current to the state variable this variable contains "old" value until next re-render

stateRef.current = formikReducer(prev, action);

And then when setFieldValue and setFieldTouched are called consecutively stateRef.current references to updated value imediatelly after setFieldValue call, but state variable is still references to the old value and setFieldTouched runs validation on that old value

Using stateRef.current directly gurantees that actual values are taken

Copy link

vercel bot commented Jan 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
formik-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 11, 2024 10:40pm

Copy link

changeset-bot bot commented Jan 25, 2024

🦋 Changeset detected

Latest commit: e004b64

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
formik Patch
formik-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codesandbox-ci bot commented Jan 25, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@ypk4
Copy link

ypk4 commented Mar 5, 2024

Helpful change. Are we pending one more review to get this merged?

@pvcresin
Copy link

Thank you very much. I hope it will be released.

@quantizor
Copy link
Collaborator

Would love a test please

@quantizor
Copy link
Collaborator

quantizor commented Apr 10, 2024

I think I see some of the source of confusion, in Formik.tsx stateRef.current is saved to state but the issue is that ref is wholly-replaced inside of dispatch. We should probably remove that variable altogether because it is a quiet source of bugs.

const state = stateRef.current;

@quantizor quantizor mentioned this pull request Apr 10, 2024
@gone-skiing
Copy link

Any chance this can get merged and released? Seems like a helpful change to keep on the shelf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants