What is the difference between formik.handleChange and formik.setFieldValue

Viewed 3548

In formik, inside the property onChange, sometimes onChange = {formik.handleChange} is called and sometimes onChange = {v => formik.setFieldValue('field', v)} is called. Whats the difference between these two?

Is it possible to replace the use of formik.setFieldValue with just formik.handleChange?

1 Answers

handleChange returns:

handleChange: (e: React.ChangeEvent<any>) => void

General input change event handler. This will update the values[key] where key is the event-emitting input's name attribute. If the name attribute is not present, handleChange will look for an input's id attribute. Note: "input" here means all HTML inputs.

setFieldValue returns:

setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void

Set values imperatively. Calling this will trigger validation to run if validateOnChange is set to true (which it is by default). You can also explicitly prevent/skip validation by passing a third argument as false.

There can find doc where differences written.

Related