英文:
How to add custom type to selected option for onChange event in React-Select
问题
我创建了一个使用react-select的SelectBox组件,并为其添加了onChange事件。
我想在onChange事件中为选定的选项添加一个类型"SelectedItemType"。
然而,onChange事件的类型是:
(property) onChange?: ((newValue: unknown, actionMeta: ActionMeta
因此,选项类型是unknown,它阻止我添加除unknown以外的类型,这使得从选项中提取对象属性变得困难。
在上面的代码中,我想将SelectedItemType添加到onChange的newValue中。然而,它抛出了一个错误,说
Type '(newValue: SelectedItemType) => void' is not assignable to type '(newValue: unknown, actionMeta: ActionMeta
Types of parameters 'newValue' and 'newValue' are incompatible.
Type 'unknown' is not assignable to type 'SelectedItemType'.
我如何将此自定义类型添加到选项中,因为我想从选项中提取值和标签?
英文:
I created a SelectBox component using react-select and added onChange event for it.
I would like to add a type "SelectedItemType" to the selected option in onChange event.
However the onChnage event is of type
(property) onChange?: ((newValue: unknown, actionMeta: ActionMeta<unknown>) => void) | undefined
Hence the option type is unknown and its preventing me from adding types other than unknown which makes it difficult to extract the object attributes from option
import { ForwardedRef, forwardRef } from "react";
import Select, { SelectInstance } from "react-select";
export type SelectedItemType = {
value: string;
label: string;
};
export type SelectProps = {
options: SelectedItemType[];
ref?: ForwardedRef<SelectInstance>;
onChange: (selectedItem: unknown) => void;
};
const SelectBox: React.FC<SelectProps> = forwardRef<
SelectInstance,
SelectProps
>(({ options, onChange, ...props }, ref) => {
return (
<Select
options={options}
ref={ref}
{...props}
onChange={(newValue: unknown) => onChange(newValue)}
/>
);
});
export default SelectBox;
In the above code I would like to add SelectedItemType to the newValue for onChange. However its throwing an error saying
Type '(newValue: SelectedItemType) => void' is not assignable to type '(newValue: unknown, actionMeta: ActionMeta<unknown>) => void'.
Types of parameters 'newValue' and 'newValue' are incompatible.
Type 'unknown' is not assignable to type 'SelectedItemType'.
How do I add this custom Type to the option as I want to extract the value and label from the option.
答案1
得分: 1
以下是翻译好的部分:
如果您想将 newValue
分配类型为 SelectedItemType
,以便在 onChange
函数中可以轻松访问其属性,那么可以通过进行两个主要更改来实现。
- 更改
SelectProps
的onChange
属性的类型如下:
export type SelectProps = {
options: SelectedItemType[];
ref?: ForwardedRef<SelectInstance>;
onChange: (selectedItem: SelectedItemType) => void;
};
- 然后,在
select
组件中,它的onChange
将被修改为:
<Select
options={options}
ref={ref}
{...props}
onChange={(newValue: unknown) => onChange(newValue as SelectedItemType)}
/>
然后像这样使用您的 SelectBox
组件:
<SelectBox
options={[
{ label: "A", value: "1" },
{ label: "B", value: "2" }
]}
onChange={function (selectedItem: SelectedItemType): void {
console.log(selectedItem.value);
}}
/>
以下是文件 App.tsx
的完整代码:
import { ForwardedRef, forwardRef } from "react";
import Select, { SelectInstance } from "react-select";
export type SelectedItemType = {
value: string;
label: string;
};
export type SelectProps = {
options: SelectedItemType[];
ref?: ForwardedRef<SelectInstance>;
onChange: (selectedItem: SelectedItemType) => void;
};
const SelectBox: React.FC<SelectProps> = forwardRef<
SelectInstance,
SelectProps
>(({ options, onChange, ...props }, ref) => {
return (
<Select
options={options}
ref={ref}
{...props}
onChange={(newValue: unknown) => onChange(newValue as SelectedItemType)}
/>
);
});
export default function App() {
return (
<div className="App">
<SelectBox
options={[
{ label: "A", value: "1" },
{ label: "B", value: "2" }
]}
onChange={function (selectedItem: SelectedItemType): void {
console.log(selectedItem.value);
}}
/>
</div>
);
}
英文:
If you would like to assign type to newValue
to SelectedItemType
so that its properties can be easily accessed in onChange
function then it can be done by doing two main changes.
- Change type of
onChange
property ofSelectProps
as
export type SelectProps = {
options: SelectedItemType[];
ref?: ForwardedRef<SelectInstance>;
onChange: (selectedItem: SelectedItemType) => void;
};
- Then in
select
component itsonChange
would be modified as
<Select
options={options}
ref={ref}
{...props}
onChange={(newValue: unknown) => onChange(newValue as SelectedItemType)}
/>
Then use your SelectBox
component like:
<SelectBox
options={[
{ label: "A", value: "1" },
{ label: "B", value: "2" }
]}
onChange={function (selectedItem: SelectedItemType): void {
console.log(selectedItem.value);
}}
/>
Here is full code for file App.tsx
import { ForwardedRef, forwardRef } from "react";
import Select, { SelectInstance } from "react-select";
export type SelectedItemType = {
value: string;
label: string;
};
export type SelectProps = {
options: SelectedItemType[];
ref?: ForwardedRef<SelectInstance>;
onChange: (selectedItem: SelectedItemType) => void;
};
const SelectBox: React.FC<SelectProps> = forwardRef<
SelectInstance,
SelectProps
>(({ options, onChange, ...props }, ref) => {
return (
<Select
options={options}
ref={ref}
{...props}
onChange={(newValue: unknown) => onChange(newValue as SelectedItemType)}
/>
);
});
export default function App() {
return (
<div className="App">
<SelectBox
options={[
{ label: "A", value: "1" },
{ label: "B", value: "2" }
]}
onChange={function (selectedItem: SelectedItemType): void {
console.log(selectedItem.value);
}}
/>
</div>
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论