gpt4 book ai didi

reactjs - 类型 'number' 不可分配给类型 'SetStateAction' 。 - react

转载 作者:行者123 更新时间:2023-12-05 09:02:46 25 4
gpt4 key购买 nike

我有一个 SelectInput 可以让我选择 1、2 或 3,在它下面,我有一个 MultiSelect(带有 Mantine 库)。

我想选择副驾驶的数量(在 SelectInput 上),并允许在 MultiSelect 上选择的数量。

这是我的代码:

const [maxCopilote, setMaxCopilote] = useState()

<NumberInput
defaultValue={1}
max={3}
min={1}
required
placeholder="Number of copilot"
onChange={(e) => setMaxCopilote(e)}
/>

<MultiSelect
data={['Copilote1', 'Copilote2', 'Copilote3']}
required
placeholder="Select copilote(s)"
maxSelectedValues={maxCopilote}
clearable
/>

使用这段代码,我得到了错误:

Argument of type 'number | undefined' is not assignable to parameter of type SetStateAction<undefined>.
Type 'number' is not assignable to type 'SetStateAction<undefined>'. TS2345

如何获取我选择的数字,并将其动态放入 maxSelectValues 中?

谢谢

PS : console.log(e) onChange in the numberInput, log the selected number correctly

最佳答案

使用useState时声明一个类型

const [maxCopilote, setMaxCopilote] = useState<number | undefined>(1)

此外,您可以添加一个默认值

关于reactjs - 类型 'number' 不可分配给类型 'SetStateAction<undefined>' 。 - react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70926039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com