Error: Objects are not valid as a React child (found: object with keys

Error: Objects are not valid as a React child (found: object with keys

大家好,能否帮助我理解我做错了什么?

此错误说明:

Error: Objects are not valid as a React child (found: object with keys {id, title, bodyText, icon}). If you meant to render a collection of children, use an array instead.

我的代码片段:

  if (cards) {
    const filteredCards = cards.filter((card: { title: string }) => {
      return card.title.toLowerCase();
    });
    return filteredCards;
  }

console.log(卡片) 给出:

cards (4) [{…}, {…}, {…}, {…}]

如果我在那里使用数组作为错误说明,这不是多余的吗?

我有点困惑。

您应该 return 一个 JSX 数组或一个字符串数组。我猜你是想获得标题的小写字母。为此,您应该使用 map 而不是 filter。当前 filteredCards 是 JSON 的数组,其格式与项目 cards.

相同

这样试试。

  if (cards) {
    const filteredCards = cards.map((card: { title: string }) => {
      return card.title.toLowerCase();
    });
    return filteredCards;
  }