T
T
TheSerKat2021-03-14 11:55:33
React
TheSerKat, 2021-03-14 11:55:33

How to map an array along with Formik's errors object?

How to map so that a class is added to className if there is an error in the erorrs object with a specific name that substitutes .map?

let fieldsContacts =[
        {id: 1, name: 'fullName'},
        {id: 2, name: 'aboutMe' },
    ]
<Formik>
 {({errors, touched => (
  <Form>
   {fieldsContacts.map(field =>
      <Field name={field.name} className={errors.field.name && touched.field.name && classes.errorField} id={field.name}/>
    )}
  </Form>
  )}
</Formik>


That is, I would like the result to be like this:
<Field name="fullName" className={errors.fullName && touched.fullName && classes.errorField} id="fullName"/>
<Field name="aboutMe" className={errors.aboutMe && touched.aboutMe && classes.errorField} id="aboutMe"/>

and so the errors object does not have undefined field.name

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-03-14
@TheSerKat

- errors.field.name
+ errors[field.name]

Also for touched.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question