[英]React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type
[英]React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction<x]>'
这是代码
试图编译它说“'toDo'类型的参数不能分配给'SetStateAction<toDo[]>'类型的参数”
我无法弄清楚它是如何不工作的,因为类型似乎是正确的。
如何避免该错误
const [todoList, setTodoList] = useState<toDo[]>([])
useEffect(() => {
loadToDoList()
}, [])
const loadToDoList = async () => {
const toDoList = await ToDoService.getToDoList()
console.log(toDoList)
setTodoList(toDoList)
}
return (
<div>
<ToDoList items={todoList} />
</div>
)
}```
此错误是因为您尝试使用类型为toDo
的变量设置状态,而它需要一个 toDo toDo[]
数组。
确认await ToDoService.getToDoList()
返回的toDoList
的类型等于toDo[]
或将useState
调用上的泛型类型声明更改为const [todoList, setTodoList] = useState<toDo>([])
,以防toDo
已经是一个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.