Search code examples
reactjstypescripttsx

React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'


Hello I'm having this error:

Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'.

this is a part of my code:

.
.
.
const[ idPadre, setIdPadre ] = useState<number>(0);
.
.
.

<select 
       onChange={ (e) => setIdPadre( e.target.value ) }
       value={ idPadre }
>
<option value="">Select...</option>
       {data.map((item) => {
                            return <option key={item.id}
                                    value={ item.id }
                            >{item.description}</option>
                            })}                                
</select>

so, I'm adding data from file json and I want that select an item and value get id.


Solution

  • The error is pretty descriptive. You cannot pass parameter of type string into the useState hook which expects number.

    You're calling the setIdPadre hook with the e.target.value variable, which is a string.

    What you can do instead is convert the value into a number:

    { (e) => setIdPadre( parseInt(e.target.value) ) }