类型“number”不可分配给类型“SetStateAction<undefined>”。- 反应

5 select webhooks reactjs react-hooks mantine

我有一个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
/>
Run Code Online (Sandbox Code Playgroud)

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

Argument of type 'number | undefined' is not assignable to parameter of type SetStateAction<undefined>.
Type 'number' is not assignable to type 'SetStateAction<undefined>'.  TS2345
Run Code Online (Sandbox Code Playgroud)

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

谢谢

PS:console.log(e)onChange中的numberInput,正确记录选择的号码

Oza*_*dul 13

使用时声明类型useState

const [maxCopilote, setMaxCopilote] = useState<number | undefined>(1)
Run Code Online (Sandbox Code Playgroud)

另外,您可以添加默认值