Formik: onChange handler doesn't work with withFormik

Created on 16 Sep 2019  路  2Comments  路  Source: formium/formik

馃悰 Bug report

Current Behavior

When using withFormik, the handleChange function is not called when passed in via props. However, the input onChange event will call a local change handler function.

Expected behavior

handleChange function from props should be called

Reproducible example

https://codesandbox.io/embed/withformik-codesandbox-template-xolcc

Your environment

| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 1.5.8
| React | 16.8.6
| TypeScript |
| Browser |
| npm/Yarn |
| Operating System | macOS

stale

Most helpful comment

This is happening to me as well using Field component

All 2 comments

This is happening to me as well using Field component

There are multiple issues going on in your sandbox. WithFormik HoC does not accept extra options, but the resulting component does accept extra _props_. However, you _cannot_ pass it a handleChange prop, because handleChange is replaced by formik's handleChange prop. Instead, you can pass a _different_ prop and use it as a change handler.

Sandbox example: https://codesandbox.io/s/withformik-codesandbox-template-j5ltm

The jist:

const MyEnhancedForm = withFormik({
  mapPropsToValues: () => ({ name: "", foo: "" }),
  handleSubmit: () => {}
})(Form);

const App = () => (
  <div className="app">
    <MyEnhancedForm
      doChange={() => console.log("prop change handler called")}
    />
  </div>
);

const Form = props => (
    <form onSubmit={props.handleSubmit}>
      Typing in this box should call onChange
      <input
        type="text"
        onChange={props.doChange}
        value={props.values.name}
        name="name"
      />
      <button type="submit">Submit</button>
      <DisplayFormikState {...props} />
    </form>
  );
Was this page helpful?
0 / 5 - 0 ratings