Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
  )
}```
like image 204
Alberto3 Avatar asked Jun 01 '26 20:06

Alberto3


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.

like image 135
Toni Bardina Comas Avatar answered Jun 04 '26 13:06

Toni Bardina Comas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!