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

[v2] Hooks #1063

Merged
merged 111 commits into from
May 29, 2019
Merged

[v2] Hooks #1063

merged 111 commits into from
May 29, 2019

Conversation

jaredpalmer
Copy link
Owner

@jaredpalmer jaredpalmer commented Oct 30, 2018

Improvements

Breaking Changes

  • Breaking TypeScript change: FormikActions have been renamed to FormikHelpers
  • Formik no longer uses console.error in its warnings, but rather console.warn

Deprecations

These are just warnings for now.

Documentation

  • useField
  • "Creating your own <FieldGroup>"
  • Checkboxes and Radio inputs
  • <FormikConsumer>
  • useFormikContext
  • useFormik
  • Rewrite examples with hooks
  • Rewrite tutorial with hooks
  • Migration Guide
  • Blog post

Feel free to start making PR's to this branch. This is going to take a minute to figure out as there are a LOT of new paradigms.

#1046 #1005

@jaredpalmer jaredpalmer changed the base branch from master to 2.0 October 30, 2018 23:14
@jaredpalmer jaredpalmer changed the base branch from 2.0 to master October 30, 2018 23:15
@weyert
Copy link

weyert commented Oct 31, 2018

Why the rename of FormikContext to FormikCtx?

@tgriesser
Copy link
Contributor

I don't have the time to make this into a formal PR right now but maybe it'll provide a little inspiration. It's a proof-of-concept of Formik working with hooks. Because of the imperative API exposed by formik, the key I found is to mostly use refs internally rather than state, otherwise the values will get stale, e.g. if you set a field and call submitForm in the same event handler. It's just a first pass, so I'm sure there's plenty of room for improvement, but it seems to work alright:

https://gist.github.com/tgriesser/fdd08da075fec80328792319606c76e5

@tgriesser tgriesser mentioned this pull request Nov 2, 2018
@jaredpalmer
Copy link
Owner Author

I (think) I'm almost done with useFormik. I made one perf improvement which I think we should push down to v1: avoid toggling isValidating if field-level validation is synchronous.

@jaredpalmer
Copy link
Owner Author

@tgriesser hrm. that's definitely a footgun with calling submit form in the same handler. i wonder if there is a way to avoid that aside from ref + force update. refs won't show up in dev tools for example.

@jaredpalmer
Copy link
Owner Author

Pretty close here. There is definitely some quirks without the state updater callback

@weyert
Copy link

weyert commented Nov 5, 2018

How can I extend it with warnings?

@jaredpalmer
Copy link
Owner Author

jaredpalmer commented Nov 5, 2018

If we decide to allow a stateReducer prop, then yes. However, that debate will come later.

@jaredpalmer jaredpalmer mentioned this pull request Nov 6, 2018
@weyert
Copy link

weyert commented Nov 26, 2018

Any way to progress this ticket? :)

@jaredpalmer jaredpalmer changed the base branch from master to next May 29, 2019 20:12
@jaredpalmer
Copy link
Owner Author

@FredyC useField is going to accept either a string name or and object with #1555

@MatyCZ
Copy link

MatyCZ commented Jun 12, 2019

Will useField accept validation too? There is no way how to add validation function to the field using the useField hook.

@danielkcz
Copy link

danielkcz commented Jun 12, 2019

@MatyCZ Is it really that hard to at least read the previous comment? 🙄 😆

@MatyCZ
Copy link

MatyCZ commented Jun 12, 2019

@FredyC Jared mentioned accepting an object with #1555 as answer to your question about validation. #1555 is already merged, but useField doesn`t accept validation as object prop.

@fabb
Copy link
Contributor

fabb commented Aug 2, 2019

Maybe remove the status functionality from the hook? Users could use the regular useState hook with the additional benefit that it‘s not of type any.

@alfred-pb
Copy link

Any progress with the docs?

@jaredpalmer
Copy link
Owner Author

I am working on docs this week

@GollyJer
Copy link

GollyJer commented Aug 20, 2019

I realize the docs aren't ready but does there happen to be a hooks code example sitting around somewhere? 😀

@alfred-pb
Copy link

alfred-pb commented Aug 21, 2019

@jaredpalmer thanks! A video will be also appreciate it 👍

@veenedu
Copy link

veenedu commented Aug 26, 2019

A demo, until we see something from @jaredpalmer

import { Formik, useFormikContext } from "formik";

const MyForm = () => {
  const {
    handleSubmit,
    handleChange,
    values,
    errors,
    isSubmitting
  } = useFormikContext();
  console.log("isSubmitting", isSubmitting);
  return (
    <div>
      <h2>Using hooks</h2>
      <form
        onSubmit={e => {
          e.preventDefault();
          handleSubmit();
        }}
      >
        <div>
          Name <input name="name" value={values.name} onChange={handleChange} />
        </div>
        {errors.name && <div>{errors.name}</div>}
        <div>
          age
          <input name="age" value={values.age} onChange={handleChange} />
          {errors.age && <div>{errors.age}</div>}
        </div>
        <div>
          <input value="submit" type="submit" disabled={isSubmitting} />
        </div>
      </form>
    </div>
  );
};

const initialValues = {
  name: "Praveen",
  age: 35
};
const config = {
  initialValues,
  onSubmit: (values, { setSubmitting }) => {
    setTimeout(() => {
      console.log("Submit", values);
      console.log("Here");
      setSubmitting(false);
    }, 1400);
  },
  validate: values => {
    const errors = {};
    if (!values.name) {
      errors["name"] = "Name is required";
    }
    if (!values.age) {
      errors["age"] = "age is required";
    }
    if (values.age && isNaN(Number(values.age))) {
      errors["age"] = "Please enter a number";
    }
    return errors;
  }
};

const Wrapped = () => (
  <Formik {...config}>
    <MyForm />
  </Formik>
);

export default Wrapped;

https://codesandbox.io/s/serene-grothendieck-se2om

@fabb
Copy link
Contributor

fabb commented Aug 26, 2019

AFAIK the Wrapped part and especially <Formik> is unnecessary when you use the hook.

@fabb
Copy link
Contributor

fabb commented Aug 26, 2019

And formik handles the preventDefault for you when you pass the event to handleSubmit:

<form onSubmit={handleSubmit}>

@veenedu
Copy link

veenedu commented Aug 26, 2019

Then how to validation rules, initial values?

@fabb

@fabb
Copy link
Contributor

fabb commented Aug 26, 2019

Ah ok, now I know the difference between useFormik and useFormikContext. The former takes the config, the latter expects being wrapped inside of a Formik Provider as you did. How confusing.

I‘m not sure if the former is meant for public use, but I think it can be used like this:

... = useFormik(config);

https:/jaredpalmer/formik/blob/master/src/Formik.tsx#L125

I think you need the provider though when you use Formik‘s <Field> (I don‘t).

@GoPro16
Copy link

GoPro16 commented Aug 26, 2019

https:/Availity/availity-react/tree/master/packages/form

Our whole reactstrap form, selects, and datepickers are built using formik v2. Probably a good production example of this. Sorry if this is frowned upon to do this until a stable release 😏

@tommedema
Copy link

@GoPro16 checked your code but don't see references to useFormik anywhere

@GoPro16
Copy link

GoPro16 commented Sep 25, 2019

@GoPro16 checked your code but don't see references to useFormik anywhere

We use the useField hook and the useFormikContext hook the most. We don't ever use useFormik because we don't need all of the formikbag, only just a sub selection like what useFormikContext returns.

https://availity.github.io/availity-react/form/components/form/

@septs
Copy link

septs commented Oct 11, 2019

@jaredpalmer release the feature to next version?

passionSeven added a commit to passionSeven/formik that referenced this pull request Oct 17, 2022
As [this comment](jaredpalmer/formik#1063 (comment)) said,  the `enableReinitialize` feature of `useFormik` hook, is missing in `v2.0.1-alpha.0`, this PR might resolve this problem.
Brwonbear9241 pushed a commit to Brwonbear9241/formilk that referenced this pull request May 5, 2023
As [this comment](jaredpalmer/formik#1063 (comment)) said,  the `enableReinitialize` feature of `useFormik` hook, is missing in `v2.0.1-alpha.0`, this PR might resolve this problem.
seniordev0312 added a commit to seniordev0312/react-form that referenced this pull request May 14, 2023
As [this comment](jaredpalmer/formik#1063 (comment)) said,  the `enableReinitialize` feature of `useFormik` hook, is missing in `v2.0.1-alpha.0`, this PR might resolve this problem.
kstar0707 added a commit to kstar0707/formik that referenced this pull request Nov 29, 2023
As [this comment](jaredpalmer/formik#1063 (comment)) said,  the `enableReinitialize` feature of `useFormik` hook, is missing in `v2.0.1-alpha.0`, this PR might resolve this problem.
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.