Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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." Why {i.title} is an object. 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'));
like image 791
Alexander Avatar asked May 08 '18 09:05

Alexander


People also ask

How do you fix objects are not valid as a React child?

In order to fix this, replace the {item} with {item.name} in the map function. This no longer attempts to render an object as a React child. Instead, it renders the name property of the object.

How do you render collection of children in React?

If you meant to render a collection of children, use an array instead. We cannot return a JavaScript object from a return call inside the render() method.

How do you pass an array of objects into a child component in React?

To pass an array as a prop to a component in React, wrap the array in curly braces, e.g. <Books arr={['A', 'B', 'C']} /> . The child component can perform custom logic on the array or use the map() method to render the array's elements. Copied!

What is a React child?

What are children? The children, in React, refer to the generic box whose contents are unknown until they're passed from the parent component. What does this mean? It simply means that the component will display whatever is included in between the opening and closing tags while invoking the component.


2 Answers

The problem is because you return

return (
        {items} 
    )

which is an equivalent of return ({items: items}) ie. you are returning an object with key items and 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>

P.S. Note that the first two approaches will work from react v16.0.0 onwards while the last will work from v16.2 onwards.

However if you are using a lower version, you would need to wrap the return element within a container like

    return (
        <div>{items}</div> 
    )
like image 147
Shubham Khatri Avatar answered Oct 04 '22 17:10

Shubham Khatri


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 ( <h1>{i.title}</h1> )
    });
    
    return (
        <div>
        {item} 
        </div>
    )
    

    } }

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

like image 42
SOUser Avatar answered Oct 04 '22 17:10

SOUser