问题
I failed to render jxs using map of lodash. I have this in my render method
return (
<div>
{map(groupedByMonths, (obj, index) =>
<div className="panel">
<div className="panel-heading">
{obj}
</div>
<div>{obj.price}</div>
</div>)}
</div>
)
But I got error of Objects are not valid as a React child The groupedByMonths structure look like this
{
"April": [
{
"price": 0,
},
{
"price": 12
},
{
"price": 200
}
]
}
回答1:
Don't know how to do this with lodash map, i can suggest this:
{Object.keys(groupedByMonths).map((obj, index) =>
<div className="panel">
<div className="panel-heading">
{obj}
</div>
{groupedByMonths[obj].map((el, i) => <div key={i}> {el.price} </div>)}
</div>)
}
When we use Object.keys(groupedByMonths) it will return an array that will contain all the keys, and we can use map on that. To print the key use {obj} and to print the values use {groupedByMonths[obj]}.
来源:https://stackoverflow.com/questions/43715974/react-map-returned-error-of-objects-are-not-valid-as-a-react-child