Here is the code
trying to compile it says "Argument of type 'toDo' is not assignable to parameter of type '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[].
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With