使用react 报 Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead…
*前段时间使用react;莫名其妙的报 Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.这个错*

render(){
return (
<div className = 'box'>
{list.map((item,index)=>(
<span>{ item }</span>
))
}
</div>
)
}
/*造成这个问题的原因就是 list数组下面有一条或者几条数组项是对象;
* 例如list 数组是[1,2,3,4,{name:"Muhammad Ali"}];
* 那么它一定会报上面的错;
*
解析的时候加上一个三目问题就解决了
*/
{list.map((item,index)=>(
<span>{ item.name===undefined?item:item.name }</span>
))
}
特此记录一下 有写的不对的地方请指正,有更好的解决办法的 请不吝赐教;咱们互相学习,共同进步
本文介绍了在使用React框架时遇到的Objects are not valid as a React child错误,并提供了解决方案。当list数组中有对象时,直接渲染会导致此错误。通过判断并正确渲染对象属性可以解决问题。

184

被折叠的 条评论
为什么被折叠?



