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'));
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.
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.
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 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.
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 fromv16.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>
)
return (
{items}
)
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.
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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With