React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction<x]>'

Viewed 775

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>
  )
}```
1 Answers

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.

Related