React render: Objects are not valid as a React child

前端 未结 4 1703
自闭症患者
自闭症患者 2020-12-21 06:03

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})

4条回答
  •  Happy的楠姐
    2020-12-21 06:27

    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 {

      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 ( 

      {i.title}

      ) }); return (
      {item}
      )

      } }

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

提交回复
热议问题