简体   繁体   中英

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

Hi guys if you can help me understand what I'm doing wrong?

This error notes:

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.

My code snippet:

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

console.log(cards) gives:

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

if I'm using an array there as the error notes, isn't this redundant?

I'm kind of confused.

You should return an array of JSX or an array of strings. I guess you meant to get the lowerCase of titles. For that, you should be using map instead of filter . Currently filteredCards is an array of JSON in the format same as an item cards .

Try like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM