Answers for "formik setfieldvalue"

1

formik setfieldvalue

useEffect(() => {
          if (!isAddMode) {
            // get user and set form fields
            userService.getById(id).then(user => {
              const fields = ['title', 'firstName', 'lastName', 'email', 'role'];
              fields.forEach(field => setFieldValue(field, user[field], false));
              setUser(user);
            });
          }
        }, []);
Posted by: Guest on August-01-2021
0

setFieldValue

const Form = ({ data }) => (
  <Formik
    initialValues={data}
    ...and do some more stuff here like error handling submit handling etc...
    render={({ values, handleSubmit, handleChange, handleBlur }) => (
      <Form onSubmit={handleSubmit}>
        <Field
          name="mail.email"
          value={values.mail.mail}
          render={({ field, form }) => (
            <input
              {...field}
              onChange={e => {
                const domain = e.target.value.replace(/.*@/, '')
                handleChange(e)
                form.setFieldValue('mail.domain', domain)
              }}
            />
          )}
        />
        <input
          name="mail.domain"
          value={values.mail.domain}
          onChange={handleChange}
          onBlur={handleBlur}
        />
       <button type="submit">Submit</button>
      </Form>
    )}
  />
)
Posted by: Guest on January-29-2021

Browse Popular Code Answers by Language