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

Make formik sync when validation functions are sync #3976

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

Conversation

fbarbare
Copy link

@fbarbare fbarbare commented Jun 25, 2024

When the given validation function are synchronous there is no reason to make them async. Making them async actually makes the whole code much slower and creates unecessary re-renders.

Copy link

vercel bot commented Jun 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 Jun 27, 2024 9:39am

Copy link

codesandbox-ci bot commented Jun 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.

Copy link

changeset-bot bot commented Jun 25, 2024

⚠️ No Changeset found

Latest commit: 1c6c3e6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@fbarbare
Copy link
Author

@jaredpalmer This change goes in the direction that you are mentioning in #2140 but is backwards compatible since it's not removing any functionality. The deprecation of the async validation methods could be done in a later stage so we can first make things more synchronous (which will improve performance of a lot of apps) and then proceed with the removal of the async stuff.
Any thoughts?

Copy link
Contributor

@bonimba bonimba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏻 Well done. Amazing update

@@ -300,26 +294,43 @@ export function useFormik<Values extends FormikValues = FormikValues>({
prev = setIn(prev, fieldKeysWithValidation[index], curr);
}
return prev;
}, {})
);
}, {} as FormikErrors<Values>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should be able to move this type in the .reduce<FormikErrors<Values>>

Suggested change
}, {} as FormikErrors<Values>);
}, {});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need it to be casted in the .reduce I think, no?

Copy link
Contributor

@bonimba bonimba Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should give you the same outcome

       fieldErrorsList.reduce<FormikErrors<Values>>((prev, curr, index) => {
          if (curr === 'DO_NOT_DELETE_YOU_WILL_BE_FIRED') {
            return prev;
          }
          if (curr) {
            prev = setIn(prev, fieldKeysWithValidation[index], curr);
          }
          return prev;
        }, {})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is does. Generics and casting are essentially the same thing though 🙁
I made the change anyway

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.

None yet

2 participants