Great article, I really enjoyed reading it! Me and my dev team always choose Formik & Yup for form handling. And I might have a suggestion that can make your code even more beautiful.
Instead of
<Form> <Field name="firstname" /> { errors.firstname && touched.firstname ? (<div>{errors.firstname}</div>) : null } </Form>
why not create a new component that will handle the errors like so
<Form> <Field name="firstname" /> <ErrorField name="firstname" /> </Form>
The ErrorField component can access the useFormikContext hook for errors and touched states.
ErrorField
const ErrorField = (name) => { const { errors, touched } = useFormikContext(); if (!errors[name] || !touched[name]) { return null; } return <div>{errors[name]}</div> }
Let me know what you think. Cheers!
That's great
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great article, I really enjoyed reading it! Me and my dev team always choose Formik & Yup for form handling. And I might have a suggestion that can make your code even more beautiful.
Instead of
why not create a new component that will handle the errors like so
The
ErrorFieldcomponent can access the useFormikContext hook for errors and touched states.Let me know what you think. Cheers!
That's great