【问题标题】:How to loop over Typescript Array in ReactJS?如何在 ReactJS 中循环 Typescript 数组?
【发布时间】:2021-11-19 02:51:54
【问题描述】:

错误:

类型 'void' 不可分配给类型 'ReactNode'.ts(2322)

index.d.ts(1354, 9):预期类型来自属性“children”,该属性在此处声明为“DetailedHTMLProps”类型

代码:

interface Item{
  item: string;
  type: string;
  answer: string;
  answers: Array<any>;
}    

interface Props {
      items: Item[];
}

//this is the line where the error is being stated
this.props.items[0].answers.forEach((item) => console.log(item));

我要做的就是遍历数组并将其注销。数组可以是数字/字符串。

我正在尝试将 props 数组分散到它自己的数组实例中,并且发生了同样的错误。

【问题讨论】:

  • 请提供其余代码。仅您的代码不会导致此类错误

标签: arrays reactjs typescript


【解决方案1】:

你没有发布足够的代码来 100% 确定,但我想说你没有从你的 React 组件返回 JSX。尝试添加

return null;

到你的反应组件的底部,看看是否有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 2021-08-27
    • 2018-04-26
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多