简体   繁体   English

我收到此错误“未捕获错误:对象作为 React 子项无效”,即使我使用了对象数组怎么办

[英]I getting this error 'Uncaught Error: Objects are not valid as a React child' even if I used Array of objects what to do

` `

const fashionCardData = [
  { id: "1", src: "1.png" },
  { id: "2", src: "2.png" },
  { id: "4", src: "4.png" },
];

const Products = () => {
  return (
    <Box>
      {fashionCardData.map((item)=>{
        <img key={item.id} src={`images/${item.src}`} alt={item.id}/>
      })}
    </Box>
  );
};

` `

I am trying to map an array but it doesn't work please help我正在尝试映射一个数组,但它不起作用请帮忙

Replace代替

{fashionCardData.map((item)=>{
    <img key={item.id} src={`images/${item.src}`} alt={item.id}/>
  })}

with

{fashionCardData.map((item)=>(
    <img key={item.id} src={`images/${item.src}`} alt={item.id}/>
  ))}

Problem is you are not returning the component.问题是您没有退回组件。

Change your code to将您的代码更改为

{fashionCardData.map((item)=>{
    return (<img key={item.id} src={`images/${item.src}`} alt={item.id}/>);
  })}

Code that is inside {} will be treated as javascript and similarly code that is inside () will be treated as jsx. {}内的代码将被视为 javascript,类似地, ()内的代码将被视为 jsx。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使我没有尝试渲染对象,获取对象也不能作为 React 子错误有效 - Getting a Objects are not valid as a React child error even though I am not trying to render an object React:Uncaught Error:对象作为React子对象无效 - React: Uncaught Error: Objects are not valid as a React child 未捕获的错误:对象作为React子元素无效,但它是一个数组 - Uncaught Error: Objects are not valid as a React child but it's an array 未捕获的错误:对象作为 React 子项无效 - Uncaught error: Objects are not valid as a React child 未捕获的错误:对象作为React子对象无效 - Uncaught Error: Objects are not valid as a React child 我不断收到此错误 Objects are not valid as a React child {} 如果您要呈现子集合,请改用数组 - I keep getting this error Objects are not valid as a React child {} If you meant to render a collection of children, use an array instead 当我使用我的反应应用程序设置反应警报时出现错误未捕获错误:对象作为 React 子项无效 - I get error when I setup react-alert with my react app Uncaught Error: Objects are not valid as a React child 出现错误:数组渲染中的“对象作为 React 子项无效” - Getting error: "Objects are not valid as a React child" on array rendering 获取有关对象的错误作为 React 子级无效 - getting error about objects are not valid as a React child 遇到错误-对象作为React子对象无效 - Getting error - Objects are not valid as a React child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM