[英]React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction<x]>'
Here is the code这是代码
trying to compile it says "Argument of type 'toDo' is not assignable to parameter of type 'SetStateAction<toDo[]>'"试图编译它说“'toDo'类型的参数不能分配给'SetStateAction<toDo[]>'类型的参数”
i cannot figure out how it is not working since the type seem to be correct.我无法弄清楚它是如何不工作的,因为类型似乎是正确的。
How to avoid that error如何避免该错误
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>
)
}```
This error is because you are trying to set the state with a variable with a type toDo
when it expects an array of toDo toDo[]
.此错误是因为您尝试使用类型为
toDo
的变量设置状态,而它需要一个 toDo toDo[]
数组。
Confirm that the type of toDoList
returned by await ToDoService.getToDoList()
is equal to toDo[]
or change your generic type declaration on the useState
call to const [todoList, setTodoList] = useState<toDo>([])
, in case toDo
is already an array.确认
await ToDoService.getToDoList()
返回的toDoList
的类型等于toDo[]
或将useState
调用上的泛型类型声明更改为const [todoList, setTodoList] = useState<toDo>([])
,以防toDo
已经是一个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.