Formik: [v2] Pass setFieldValue in useField's returned value.

Created on 10 Aug 2019  路  5Comments  路  Source: formium/formik

馃殌 Feature request

Passing setFieldValue in useField's meta returned value would be interesting for checkboxes.

An example:
```js
const Checkbox = ({ name }) => {
const [field, meta] = useField(name)
return {
type="checkbox"
checked={field.value}
name={field.name}
onChange={() => meta.setFieldValue(field.name, !field.value)}
/>
}
}

stale

Most helpful comment

@sumgwork you can use useFormikContext and get setFieldValue from that

All 5 comments

Checkboxes are now handled out of the box though.

Uh I should start reading the change logs. I guess it would still be useful for using with other libraries like react-select, but I guess that might be an edge cases you don't want to cover.

Any chance of this to be reconsidered? It would be a great addition exactly for the reason @Ericnr mentioned above. Now I have to pass setFieldValue and setFieldTouched to every select because react-select handles event onChange differently.

<Select
  name="instanceType"
  setFieldValue={setFieldValue}
  setFieldTouched={setFieldTouched}
  ...
/>

I am creating a material UI based components library, and want to integrate some custom form elements with Formik 2. As of now I have to pass setFieldValue and setFieldTouched manually when I use the useFormik hook to obtain form props. If these values can be provided by the hook itself, it can save the trouble of passing these values down the component chain.

@sumgwork you can use useFormikContext and get setFieldValue from that

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dfee picture dfee  路  3Comments

outaTiME picture outaTiME  路  3Comments

Jucesr picture Jucesr  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments