简体   繁体   English

React render:对象作为React子对象无效

[英]React render: Objects are not valid as a React child

Please help me. 请帮我。 I don't know why this piece of code doesn't work. 我不知道为什么这段代码不起作用。

It gives me an error: "Objects are not valid as a React child (found: object with keys {itemss}). If you meant to render a collection of children, use an array instead." 它给了我一个错误:“对象无效作为React子对象(找到:具有键{objectss}的对象)。如果你想渲染一个子集合,请使用数组。” Why {i.title} is an object. 为什么{i.title}是一个对象。 It's just a string. 这只是一个字符串。 How can i fix this? 我怎样才能解决这个问题? and How actually I can iterate nested objects? 我实际上如何迭代嵌套对象?

class ShopItem extends React.Component {

render() {
    var items = [
        {
            link: 'first link',
            title: 'Emery NEM XF',
            price: '$950'
        },
        {
            link: 'second link',
            title: 'Emery NEM XF',
            price: '$950'
        },
        {
            link: 'third link',
            title: 'Emery NEM XF',
            price: '$950'
        }
    ];


     const item = items.map((i) =>{

          return ( <h1>{i.title}</h1> )
    });

        return (
            {items} 
        )
  }
}

ReactDOM.render(<ShopItem /> , document.querySelector('#root'));

The problem is because you return 问题是因为你回来了

return (
        {items} 
    )

which is an equivalent of return ({items: items}) ie. 这相当于return ({items: items})即。 you are returning an object with key items and React doesn't expect objects for rendering . 您正在返回一个包含关键items的对象,而React doesn't expect objects for rendering You could either write 你可以写

   const items = items.map((i) =>{
      return ( <h1>{i.title}</h1> )
   });

   return items;

or 要么

     return items.map((i) =>{
        return ( <h1>{i.title}</h1> )
     });

or 要么

  const items = items.map((i) =>{
      return ( <h1>{i.title}</h1> )
   });

  return <React.Fragment>
        {items} 
    </React.Fragment>

PS Note that the first two approaches will work from react v16.0.0 onwards while the last will work from v16.2 onwards. PS注意,前两种方法将工作从react v16.0.0 onwards而最后将工作从v16.2起。

However if you are using a lower version, you would need to wrap the return element within a container like 但是,如果您使用的是较低版本,则需要将返回元素包装在容器中

    return (
        <div>{items}</div> 
    )

You need to wrap items in JSX in your return statement: 您需要在return语句中将items包装在JSX中:

return (
    <React.Fragment>
        {items}
    </React.Fragment>
)

Your final return statement currently is not inside JSX. 你的最终return语句目前不在JSX中。 It's plain old JavaScript that is constructing an object like {items: items} thanks to enhanced object literals , which is why React is saying you are giving it an item with key items . 由于增强的对象文字 ,这是一个简单的旧JavaScript构建像{items: items}这样的对象 ,这就是为什么React说你给它一个带有关键items

here you go: 干得好:

return (
    <div>
        {items.map(i) => (
            <h1>{i.title}</h1>
        )}
    </div>
)

edit: in react, return/render can not have a structure like 编辑:在反应中,返回/渲染不能有像这样的结构

<h1>{i.title}</h1>
<h1>{i.title}</h1>
<h1>{i.title}</h1>

you should wrap them in a <div> or another element. 你应该将它们包装在<div>或其他元素中。 also you JS was wrong which I corrected 你纠正的JS也错了

return (
        {items} 
    )
  1. In the above line, you render the "items" list. 在上面的行中,您将呈现“项目”列表。 You should render the "item" list which you have create from the map function of the "items" list. 您应该从“项目”列表的地图功能中呈现您创建的“项目”列表。

  2. And you should be render it into an element. 你应该把它渲染成一个元素。

    class ShopItem extends React.Component { class ShopItem扩展了React.Component {

    render() { var items = [ { link: 'first link', title: 'Emery NEM XF', price: '$950' }, { link: 'second link', title: 'Emery NEM XF', price: '$950' }, { link: 'third link', title: 'Emery NEM XF', price: '$950' } ]; render(){var items = [{link:'first link',title:'Emery NEM XF',price:'$ 950'},{link:'second link',title:'Emery NEM XF',price:' $ 950'},{link:'第三链接',标题:'Emery NEM XF',价格:'$ 950'}];

      const item = items.map((i) =>{ return ( <h1>{i.title}</h1> ) }); return ( <div> {item} </div> ) 

    } } }}

ReactDOM.render( , document.querySelector('#root')); ReactDOM.render(,document.querySelector('#root'));

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

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