我正在使用react-hook-form控制器和react-select。我正在将 React 与Typescript一起使用。我可以在提交时获取 selected 的值,但它从选项数组中返回值和标签作为对象。我怎样才能只返回值?这是代码,
const { handleSubmit, control, formState:{errors}} = useForm<IAddProductForm>();
const handleOnSubmit = (data:IAddProductForm) => {
console.log(data);
}
const categoryOptions = [
{value:"Grocery", label:"Grocery"},
{value:"Pharmacy", label:"Pharmacy"},
{value:"Electronic", label:"Electronic"},
{value:"Food", label:"Food"},
];
return(
<React.Fragment>
<Form onSubmit={handleSubmit(handleOnSubmit)}>
<Controller
control={control}
render={({field:{onChange, value, name, ref}}) => (
<Select
inputRef={ref}
value={categoryOptions.find(c => c.value === value)}
options={categoryOptions}
onChange={onChange}
/>
)}
name={"category"}
/>
<Button type={"submit"}>submit</Button>
</Form>
</React.Fragment>
);
category: {value: "Grocery", label: "Grocery"}
如果我选择 Grocery 并提交,此代码将返回此代码。但我需要category:"Grocery"
像这样返回。