Javascript 如何在我使用的选择选项值中添加对象数组
我使用react select multiple,该选项是一个对象,但我无法发布正确的数据 这是我在react和axios中使用formik的代码 这是我的提交方法Javascript 如何在我使用的选择选项值中添加对象数组,javascript,reactjs,axios,formik,react-select,Javascript,Reactjs,Axios,Formik,React Select,我使用react select multiple,该选项是一个对象,但我无法发布正确的数据 这是我在react和axios中使用formik的代码 这是我的提交方法 const handleOnSubmit = (values, actions) => { console.log('Form Data',values); alert(JSON.stringify(values, null, 2)) confirmAlert({
const handleOnSubmit = (values, actions) => {
console.log('Form Data',values);
alert(JSON.stringify(values, null, 2))
confirmAlert({
title: 'Confirmer pour soumettre',
message: 'êtes-vous sûr de le faire.',
buttons: [
{
label: 'Oui',
onClick: () => {
axios.post("/prescribes", {
drugs: values.drugs,
nom: values.nom
}
)
.then(response => {
actions.setSubmitting(false);
console.log(response.data);
actions.resetForm();
})
.catch(error => {
actions.setSubmitting(false);
handleServerResponse(false, error.response.data.error);
});
alert('Click Oui')
}
},
{
label: 'Non',
onClick: () => alert('Click Non')
}
]
});
};
这是使用Formik的表单
<Formik
initialValues={{
nom: '',
drugs: [{}],
}}
onSubmit={handleOnSubmit}
validationSchema={formSchema}
>
{({isSubmitting, values, errors, touched, setFieldValue}) => (
<Form id="fs-frm" noValidate>
.....
<Select
name="drugs"
closeMenuOnSelect={false}
options={drug.map((e) => ({label: e.nom, value: e.id}))}
isMulti
values={values.drugs}
onChange={(
selectedValue /* check this value before passing to the setFieldValue */
) => setFieldValue('drugs', selectedValue)}
/>
.....
<Input
type="text"
name="nom"
autoCorrect="off"
autoComplete="name"
placeholder="Nom"
onChange={(
yourValue /* check this value before passing to the setFieldValue */
) => setFieldValue('nom', yourValue)}
valid={touched.nom && !errors.nom}
error={touched.nom && errors.nom}
/>
</Form>
)}
</Formik>;
我的表单就像图片中的输入
我的问题是如何在formik值中添加正确的数据?数据它是一个对象数组如何将console的结果更改为服务器的结果?也许可以看看这些解决方案,了解如何在GitHub上使用。我对Formik或React Select了解不够,无法给你这个问题一个正确的答案,但请检查一下。虽然“druge.map”应该是“drugs.map”,但看起来您可能映射了错误的数组?再说一次,我知道的还不够,但也许那些在GitHub上的人知道:)
"drugs": [
{
"id": null,
"nom": null,
"brand": null,
"indication": null,
"contraindication": null
},
{
"id": null,
"nom": null,
"brand": null,
"indication": null,
"contraindication": null
}
]