clone this repo
cd formik
yarn developinitialValuesinitialValuesare like states in formik,initialValuesmust be refereced to inputs as id or name.
propspropscontains the event handling functions in formik, we can pass it as props properties, e.g:- handleChange.
handleChangehandleChangeis the event handler function in formik. This function handle's the values, while changing the values in input fields.
handleSubmithandleSubmitis used to handle the submit action in formik.
valuesvaluesare refers to the perticular field value in initialValues.
-
FormFormis used to handle onSubmit event in Formik.
-
Field- Form inputs can replace with
Fieldin Formik. Name or Id must be match with initial values inField.
- Form inputs can replace with
-
setFieldValue-
Instead of using
handleChangeyou could usesetFielValuein cases where you want to explicitly set the particular field value.setFieldValuecan be used in form element like this: -
It also can be used out of form elements and in custom components like
react-rating(in setting the ratings field based on what user clicked) orGoogle-maps(in setting the co-ordinates of the location the user clicked)
-
Yupyupis used to validate form fields.
npm i yup (or) yarn add yupErrorMessageErrorMessageis used to simplify error message in Formik.ErrorMessageneed Name or Id for the error Field, We can set a component as div or span.
- Always convert single line values to lowercase.
- Use
Disabledoption to the submit button during submission to avoid multiple clicks.