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

Added external subscriptions. #12

Open
wants to merge 1 commit into
base: johnrom/v3
Choose a base branch
from

Conversation

johnrom
Copy link
Owner

@johnrom johnrom commented Apr 13, 2022

Added useFormikSubscriptionByRef() hook to subscribe to changes to Formik externally, via a ref.

Used like this:

const MyForm = () => {
  const formikRef = React.useRef(null);
  const values = useFormikSubscriptionByRef(
    formikRef,
    initialValues,
    React.useCallback((s) => s.values, [])
  );

  return (
    <div>
      <h1>External Formik Subscriptions</h1>
      <Formik
        innerRef={formikRef}
        initialValues={initialValues}
        onSubmit={(values) => console.log(values)}
      >
        <Field name="name" />
      </Formik>
      <pre>
        <code>{JSON.stringify(values, null, 2)}</code>
      </pre>
    </div>
  )
}

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.

1 participant