jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.71k stars 2.77k forks source link

feat: Added ability to provide patching function to setStatus method #3931

Open dan-irf opened 6 months ago

dan-irf commented 6 months ago

Problem

When setting status, sometimes user needs to update the existing value. When used inside a useCallback hook for instance, it introduces an unnecessary variable to the dependency list.

Solution

setStatus now acepts a patching function

changeset-bot[bot] commented 6 months ago

⚠️ No Changeset found

Latest commit: ede339914e01a160d9aef10c2231deb4e5569a6d

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

vercel[bot] commented 6 months ago

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 Dec 22, 2023 2:01pm
codesandbox-ci[bot] commented 6 months ago

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.

Latest deployment of this branch, based on commit ede339914e01a160d9aef10c2231deb4e5569a6d:

Sandbox Source
Formik TypeScript Playground (forked) Configuration
dan-irf commented 6 months ago

@quantizor @johnrom @jaredpalmer just wanted to ask you for PR review explicitly

johnrom commented 2 months ago

I'm not sure what the purpose of this change is. Are you creating a new status based on the last status? It's not really clear by default where this status comes from (render or state), so I would expect Formik (if it is being brought back from the dead) to move in a direction where the source of this value can be determined explicitly, either within a render using useFormikContext().status, or the absolute current value which may be "ahead" of the render, using a getState()-like API, similar to redux.

const { setStatus, getState, status } = useFormikContext();

useEffect(() => { setStatus(status === "value in current render" ? "a" : "b"); }, [setStatus, status]);
useEffect(() => { setStatus(getState().status === "absolutely current value" ? "a" : "b"); }, [setStatus, getState]);

You can see what that API could look like here: #3231 (the file doesn't load initially, so check Formik.tsx -> useFormik -> useFormikSubscription()).