react map returned error of Objects are not valid as a React child

余生颓废 提交于 2019-12-25 09:19:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!