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 UseField type definition, add micro-hooks. #3125

Conversation

johnrom
Copy link
Collaborator

@johnrom johnrom commented Mar 25, 2021

This adds the convenience hooks from the original v3.

It doesn't add useFieldX, useSetFieldX hooks, because users should simply use useFieldMeta() and the relevant API methods from useFormikContext(). It is not going to have a noticeable performance difference. useFieldMeta provides the same functionality as using all the useFieldX() combined.

johnrom and others added 26 commits March 8, 2021 18:28
Added Ref State.
Added useSelectorComparer.
Starting to build subscriptions.
use-subscriptions might not work in React 17? Seems returning the previous value doesn't bail out the render.
Sync up formik-native and formik for v3.
…te will do. If we call our own reducer then use useReducer dispatch, `state.values !== getState().values`.
…ormikReducerState + FormikComputedState.

Add Fixtures and Tutorial code to /app.
Consolidate State and Add Tutorial + Fixtures.
@johnrom johnrom requested a review from jaredpalmer as a code owner March 25, 2021 18:31
@changeset-bot
Copy link

changeset-bot bot commented Mar 25, 2021

🦋 Changeset detected

Latest commit: 00dac43

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

This PR includes changesets to release 3 packages
Name Type
formik-native Major
formik Major
app Major

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

@vercel
Copy link

vercel bot commented Mar 25, 2021

@johnrom is attempting to deploy a commit to the Formium Team on Vercel.

A member of the Team first needs to authorize it.

@johnrom johnrom mentioned this pull request Mar 25, 2021
1 task
@dantman
Copy link

dantman commented Mar 26, 2021

It doesn't add useFieldX, useSetFieldX hooks, because users should simply use useField(). It is not going to have a noticeable performance difference. useField provides the same functionality as using all the useFieldX() combined.

Doesn't useField declare the field? I use useFieldValue and useFieldSetValue in various places other than the field itself. e.g. A Collapse component that depends on the value of a checkbox. An effect that resets the value of a field when conditions change in a way that will make the field become invalid. etc.

@johnrom
Copy link
Collaborator Author

johnrom commented Mar 26, 2021

You can use useFieldMeta for this purpose which supplies the underlying state.

Updated my description above.

@dantman
Copy link

dantman commented Mar 26, 2021

Ok, fine. Though this does look a lot uglier.

const { setFieldValue } = useFormikContext();
const { value: otherValue } = useFieldMeta(props.otherFieldName);

useEffect(() => {
  console.log(otherValue);
  setFieldValue(props.otherFieldName, 'foo');
}, [otherValue, setValue, props.otherFieldName]);

Than the previous v3 code:

const [otherValue, setOtherValue] = useFieldValue(props.otherFieldName);

useEffect(() => {
  console.log(otherValue);
  setOtherValue('foo');
}, [otherValue, setOtherValue]);

@johnrom
Copy link
Collaborator Author

johnrom commented Mar 27, 2021

I agree it could be more concise. The previous concise API had a reason - the smallest possible hooks were necessary to get optimized subscriptions.

This one doesn't need it, and in fact using smaller subscriptions may have a performance hit because each one is a subscription. I think a balance of this would be to return Formik API from useFieldMeta:

const [fieldMeta, api] = useFieldMeta(name);

johnrom added 8 commits April 12, 2021 13:50
…API.

Moved FieldHelpers to their own hooks.
Removed batchedUpdates since we use an Effect.
Some minor type fixes, code and documentation cleanup.
…ions

# Conflicts:
#	packages/formik/src/Formik.tsx
…hooks-and-field-fixes

# Conflicts:
#	packages/formik/src/Field.tsx
#	packages/formik/src/hooks/hooks.ts
#	packages/formik/src/utils.ts
@github-actions
Copy link
Contributor

This pull request is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label May 14, 2021
@johnrom johnrom removed the stale label May 14, 2021
@johnrom
Copy link
Collaborator Author

johnrom commented Jun 7, 2021

closed in favor of #3231

@johnrom johnrom closed this Jun 7, 2021
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.

3 participants