'Type 'number' is not assignable to type 'SetStateAction<undefined>'. - React

I have a SelectInput that let me choose 1, 2 or 3, and below that I have a MultiSelect (with Mantine library).

I would like to select the number of co-pilot (on the SelectInput), and allow the selected number on the MultiSelect.

Here is my code :

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
/>

With this code, I got the error :

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

How can I get the number I selected, to put it dynamicaly into maxSelectValues ?

Thank you

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



Solution 1:[1]

declare type when using useState

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

also we may add default value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ozan Mudul