React formik form validation: How to initially have submit button disabled

前端 未结 3 1521
孤城傲影
孤城傲影 2021-02-05 08:20

Below is my React form validation code in which I am using formik. By default when the form loads, I want to keep the submit button disabled:



        
3条回答
  •  情话喂你
    2021-02-05 09:24

    Formik keeps track of field values and errors however exposes them for your use, this used to be done via formProps using the render props pattern however now seems to be part of the formik variable returned by the useFormik hook.

    I would recomend to start by removing the initial values to a constant. Then you need to access the formik's error object. I have not done this using the new hook syntax, however, looking at the docs I would expect "formik.errors" to work (this is exposed in formProps.errors using render props). Finally the submit buttion disabled should be a check that either formik.values is equal to the initial values OR the errors object is not empty.

提交回复
热议问题