简体   繁体   English

React js Typescript “字符串”类型的参数不可分配给“SetStateAction”类型的参数<number> '</number>

[英]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.所以,我从文件 json 添加数据,我希望 select 是一个项目和值获取 id。

The error is pretty descriptive.该错误非常具有描述性。 You cannot pass parameter of type string into the useState hook which expects number.您不能将string类型的参数传递到需要数字的 useState 挂钩中。

You're calling the setIdPadre hook with the e.target.value variable, which is a string.您正在使用e.target.value变量调用setIdPadre挂钩,该变量是一个字符串。

What you can do instead is convert the value into a number:您可以做的是将值转换为数字:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;x&#39;类型的React Typescript参数不可分配给&#39;SetStateAction&#39;类型的参数<x]> &#39; - React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction<x]>' 类型错误:“字符串”类型的参数不可分配给“SetStateAction”类型的参数<number> '?</number> - Type error: Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'? React Typescript - 类型参数不可分配给“SetStateAction”类型的参数<user | record<string, never> >'</user> - React Typescript - Argument of type is not assignable to parameter of type 'SetStateAction<User | Record<string, never>>' Typescript/React-Native:'string | 类型的参数 null' 不可分配给“SetStateAction”类型的参数<never[]> '</never[]> - Typescript/React-Native: Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction<never[]>' “number[]”类型的参数不可分配给“SetStateAction”类型的参数<ICoord | undefined> &#39; - Argument of type 'number[]' is not assignable to parameter of type 'SetStateAction<ICoord | undefined>' “文件”类型的参数不可分配给“SetStateAction”类型的参数<string> '</string> - Argument of type 'File' is not assignable to parameter of type 'SetStateAction<string>' 如何修复 React TypeScript 错误:'""' 类型的参数不可分配给 'SetStateAction 类型的参数<undefined> '</undefined> - How to fix React TypeScript error: Argument of type '""' is not assignable to parameter of type 'SetStateAction<undefined>' 类型参数不可分配给“SetStateAction”类型的参数<never[]> &#39;。 在反应中 - Argument of type is not assignable to parameter of type 'SetStateAction<never[]>'. in React “未知”类型的参数不可分配给“SetStateAction”类型的参数<string> '</string> - Argument of type 'unknown' is not assignable to parameter of type 'SetStateAction<string>' “响应”类型的参数不可分配给“SetStateAction”类型的参数 - Argument of type 'Response' is not assignable to parameter of type 'SetStateAction`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM