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

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

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

Conversation

dan-irf
Copy link

@dan-irf dan-irf commented Dec 22, 2023

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

Copy link

changeset-bot bot commented Dec 22, 2023

⚠️ No Changeset found

Latest commit: ede3399

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

Copy link

vercel bot commented Dec 22, 2023

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

Copy link

codesandbox-ci bot commented Dec 22, 2023

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 ede3399:

Sandbox Source
Formik TypeScript Playground (forked) Configuration

@dan-irf
Copy link
Author

dan-irf commented Dec 26, 2023

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

Copy link
Collaborator

@quantizor quantizor left a comment

Choose a reason for hiding this comment

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

Can you add a patch changeset please?

@johnrom
Copy link
Collaborator

johnrom commented Apr 10, 2024

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

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

3 participants