简体   繁体   English

&#39;x&#39;类型的React Typescript参数不可分配给&#39;SetStateAction&#39;类型的参数<x]> &#39;

[英]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.

相关问题 React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 - React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type React js Typescript “字符串”类型的参数不可分配给“SetStateAction”类型的参数<number> '</number> - React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>' 如何修复 React TypeScript 错误:'""' 类型的参数不可分配给 'SetStateAction 类型的参数<undefined> '</undefined> - How to fix React TypeScript error: Argument of type '""' is not assignable to parameter of type 'SetStateAction<undefined>' Typescript/React-Native:'string | 类型的参数 null' 不可分配给“SetStateAction”类型的参数<never[]> '</never[]> - Typescript/React-Native: Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction<never[]>' 类型参数不可分配给“SetStateAction”类型的参数<never[]> &#39;。 在反应中 - Argument of type is not assignable to parameter of type 'SetStateAction<never[]>'. in React “响应”类型的参数不可分配给“SetStateAction”类型的参数 - Argument of type 'Response' is not assignable to parameter of type 'SetStateAction` 打字稿:X 类型的参数不能分配给 Y 和 X 类型的参数。X 类型不能分配给 Y 类型 - Typescript: Argument of type X is not assignable to parameter of type Y & X. Type X is not assignable to type Y Typescript 抱怨 React useState setter `Argument of type 'Dispatch <setstateaction<never[]> &gt;' 不可分配给参数</setstateaction<never[]> - Typescript complaining about React useState setter `Argument of type 'Dispatch<SetStateAction<never[]>>' is not assignable to parameter of Typescript 错误:X 类型的参数不能分配给 Y 类型的参数 - Typescript error: Argument of Type X is not assignable to parameter of type Y React Typescript - 类型参数不可分配给类型参数 - React Typescript - Argument of type is not assignable to parameter of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM