formik icon indicating copy to clipboard operation
formik copied to clipboard

feat: Added ability to provide patching function to setStatus method

Open dan-irf opened this issue 2 years ago • 5 comments

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

dan-irf avatar Dec 22 '23 13:12 dan-irf

⚠️ 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

changeset-bot[bot] avatar Dec 22 '23 13:12 changeset-bot[bot]

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

vercel[bot] avatar Dec 22 '23 13:12 vercel[bot]

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

codesandbox-ci[bot] avatar Dec 22 '23 13:12 codesandbox-ci[bot]

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

dan-irf avatar Dec 26 '23 12:12 dan-irf

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()).

johnrom avatar Apr 10 '24 22:04 johnrom