'How to pass id inside values object in Formik
Am showing firstName, lastName and Role of user on the <select> and <option>.
Although those are the values am displaying on the form, I want to assign user.id to assignedTo property before I submit the values. How do I map user.id to assignedTo?
const Common = ({handleSubmit}) => {
return (
<Formik
enableReinitialize
initialValues={{
assignedTo: ""
}}
validationSchema={FormSchema}
onSubmit={(values) => {
handleSubmit(values)
}}
>
{({
handleChange,
handleSubmit,
}) => {
return (
<Form>
<Button type="submit">
{'Submit'}
</Button>
<div>
<InputLabel
htmlFor="assignedTo"
className={classes.inputLabel}
>
{'Assigned To'}
</InputLabel>
<select
onChange={handleChange}
name="assignedTo"
value={values.assignedTo}
>
<option value={values.assignedTo} selected>
{values.assignedTo}
</option>
{userData
.map((user) => (
<option
value={
`${user.firstName}
${user.lastName}
(${user.role.description})`
}
key={user.id}>
{
`${user.firstName}
${user.lastName}
(${user.role.description})
`
}
</option>
))}
</select>
</div>
</Form>
);
}}
</Formik>
);
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
