[英]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.