我有一个使用它的 formik 助手我在表单上管理我的状态我的问题是如何在 onChange 方法上更改组件状态我尝试了类似下面的方法但它没有成功!任何人都可以帮助我.. 每当用户触发 onChange 方法时,我想获取它的值并通过该值请求 api 并在表单上添加新的选择...非常感谢
** custom formik lib
const MaterialUISelectField = ({
errorString,
label,
children,
value,
name,
onChange,
onBlur,
required,
}) => {
return (
<FormControl fullWidth>
<InputLabel required={required}>{label}</InputLabel>
<Select
name={name}
onChange={onChange}
onBlur={onBlur}
value={value}
>
{children}
</Select>
<FormHelperText error={true}> {errorString}</FormHelperText>
</FormControl>
);
};
const FormikSelect = ({ name, items, label, required = false, error }) => {
return (
<div className="FormikSelect">
<Field
name={name}
as={MaterialUISelectField}
label={label}
errorString={error ? error : <ErrorMessage name={name} />}
required={required}
>
<MenuItem disabled value="">
<em>{label} Seçiniz</em>
</MenuItem>
{items != null && items.map(item => (
<MenuItem key={item.Id} value={item.Id}>
{item.Ad}
</MenuItem>
))}
</Field>
</div>
);
};
** component where I use the custom formik lib
const [state, setState] = React.useState('');
<FormikSelect
name="aa"
label="aa"
items={data}
onChange={(e) => setState(e.target.value)} />