类型“ Response”的参数不能分配给类型“ SetStateAction”的参数

Yogendra

这是我的CommentResponse接口,它是API响应中数组内部对象的结构

interface CommentResponse {
  body: string;
  email: string;
  id: number;
  name: string;
  postId: number;
}

使用setAllComments(useState-hook)从API响应设置数据时,出现以下错误

/typescript-demo/src/components/Dashboard/index.tsx
TypeScript error in /typescript-demo/src/components/Dashboard/index.tsx(35,20):
Argument of type 'Response' is not assignable to parameter of type 'SetStateAction<CommentResponse[]>'.
  Type 'Response' is missing the following properties from type 'CommentResponse[]': length, pop, push, concat, and 28 more.  TS2345
    32 |     comments = await comments.json();
    33 |     console.log(comments);
    34 |     // comments = await JSON.parse(comments);
  > 35 |     setAllComments(comments);
       |                    ^
    36 |   };
    37 | 
    38 |   useEffect(() => {

@RajaJaganathan

const [allComments, setAllComments] = useState<CommentResponse[]>([]);

  const commentService = async () => {
    const COMMENTS_API = `https://jsonplaceholder.typicode.com/comments`;    
    let comments = await fetch<Array<CommentResponse>>(COMMENTS_API);

    comments = await comments.json();
    console.log(comments);

    setAllComments(comments);
  };

我访问了其他StackOverflow和Google链接,但无法找到解决方案

贾格纳斯·斯旺卡

在您的情况下,来自fetch的响应将返回Object类型为value的对象Response {type: "cors", url: "https://jsonplaceholder.typicode.com/comments", redirected: false, status: 200…}

将界面放置在正确的位置:

const COMMENTS_API = `https://jsonplaceholder.typicode.com/comments`;    
let comments =  await fetch(COMMENTS_API);

let result: Array<CommentResponse> = await comments.json();

setAllComments(result);

您可以同时使用typeresult:Array<CommentResponse>result:CommentResponse[]

在此处查看代码演示以获取和获取axios。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

参数类型“对象”不能分配给参数类型“ System.Windows.Forms.Control”

来自分类Dev

参数类型“ A”不能分配给类型“ B”吗?

来自分类Dev

尽管参数类型继承,但不能分配给参数类型

来自分类Dev

参数类型“ CheckIn”不能分配给参数类型“()-> CheckIn”

来自分类Dev

参数类型不能分配给参数类型,应为

来自分类Dev

类型'this'的参数不能分配给'工具'类型的参数

来自分类Dev

Angular + Chart.js /类型'ChartPoint'的参数不能分配给'number&ChartPoint'类型的参数

来自分类Dev

Typescript枚举键入错误(类型'string'的参数不能分配给类型的参数)

来自分类Dev

React useState hooks错误:无法将类型“ xxx”的参数分配给类型“ SetStateAction <xx>”的参数

来自分类Dev

类型'“”'的参数不能分配给类型'“ prototype”'茉莉的参数

来自分类Dev

React Typescript-类型的参数不能分配给类型的参数

来自分类Dev

参数类型'GeneratedIntColumn'不能分配给参数类型'int':Moor-Flutter

来自分类Dev

“字符串类型的参数不能分配给自定义类型的参数”的难看的问题

来自分类Dev

“日期|类型”的参数 “ null”不可分配给“ SetStateAction <Date>”类型的参数

来自分类Dev

类型“ X”的参数不能分配给类型“ Y”的参数

来自分类Dev

类型“ {}”的参数不能分配给Angular 8中的类型的参数

来自分类Dev

导致ts错误。类型““ long”'的参数不能分配给类型'TextDataTypeOptions'的参数

来自分类Dev

类型'number'的参数不能分配给array.includes()中类型'never'的参数

来自分类Dev

通用扩展参数不能分配给参数类型

来自分类Dev

参数类型'jsObject'不能分配给'buildContext'参数吗?

来自分类Dev

用Typescript进行反应:类型'never []'的参数不能分配给类型'StateProperties |的参数。(()=> StateProperties)'

来自分类Dev

“预订”类型的参数不能分配给“功能”类型的参数

来自分类Dev

类型的参数不能分配给类型“ MiddlewareFunc”的参数。app.use(oakCors());

来自分类Dev

ts(2345)类型'any'的参数不能分配给'never'类型的参数

来自分类Dev

类型'InputType []'的参数不能分配给类型'GenericType []'的参数

来自分类Dev

参数类型'Future <>'不能分配给参数类型''

来自分类Dev

“1”类型的参数不能分配给“number[]”类型的参数

来自分类Dev

'true' 类型的参数不能分配给类型的参数

来自分类Dev

“Params”类型的参数不能分配给“string”类型的参数

Related 相关文章

  1. 1

    参数类型“对象”不能分配给参数类型“ System.Windows.Forms.Control”

  2. 2

    参数类型“ A”不能分配给类型“ B”吗?

  3. 3

    尽管参数类型继承,但不能分配给参数类型

  4. 4

    参数类型“ CheckIn”不能分配给参数类型“()-> CheckIn”

  5. 5

    参数类型不能分配给参数类型,应为

  6. 6

    类型'this'的参数不能分配给'工具'类型的参数

  7. 7

    Angular + Chart.js /类型'ChartPoint'的参数不能分配给'number&ChartPoint'类型的参数

  8. 8

    Typescript枚举键入错误(类型'string'的参数不能分配给类型的参数)

  9. 9

    React useState hooks错误:无法将类型“ xxx”的参数分配给类型“ SetStateAction <xx>”的参数

  10. 10

    类型'“”'的参数不能分配给类型'“ prototype”'茉莉的参数

  11. 11

    React Typescript-类型的参数不能分配给类型的参数

  12. 12

    参数类型'GeneratedIntColumn'不能分配给参数类型'int':Moor-Flutter

  13. 13

    “字符串类型的参数不能分配给自定义类型的参数”的难看的问题

  14. 14

    “日期|类型”的参数 “ null”不可分配给“ SetStateAction <Date>”类型的参数

  15. 15

    类型“ X”的参数不能分配给类型“ Y”的参数

  16. 16

    类型“ {}”的参数不能分配给Angular 8中的类型的参数

  17. 17

    导致ts错误。类型““ long”'的参数不能分配给类型'TextDataTypeOptions'的参数

  18. 18

    类型'number'的参数不能分配给array.includes()中类型'never'的参数

  19. 19

    通用扩展参数不能分配给参数类型

  20. 20

    参数类型'jsObject'不能分配给'buildContext'参数吗?

  21. 21

    用Typescript进行反应:类型'never []'的参数不能分配给类型'StateProperties |的参数。(()=> StateProperties)'

  22. 22

    “预订”类型的参数不能分配给“功能”类型的参数

  23. 23

    类型的参数不能分配给类型“ MiddlewareFunc”的参数。app.use(oakCors());

  24. 24

    ts(2345)类型'any'的参数不能分配给'never'类型的参数

  25. 25

    类型'InputType []'的参数不能分配给类型'GenericType []'的参数

  26. 26

    参数类型'Future <>'不能分配给参数类型''

  27. 27

    “1”类型的参数不能分配给“number[]”类型的参数

  28. 28

    'true' 类型的参数不能分配给类型的参数

  29. 29

    “Params”类型的参数不能分配给“string”类型的参数

热门标签

归档