简体   繁体   中英

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[]>'"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM