|
React 项目中,遇到了如下错误:
Objects are not valid as a React child代码如下: let commentArr = [
{"authorName":'张三','date':'2008-8-8','message':'我要发财了'},
{"authorName":'李四','date':'2021-8-8','message':'我要起飞了'},
{"authorName":'王五','date':'2021-6-6','message':'我要走运了'}, {"authorName":'赵六','date':'2021-9-9','message':'我要脱单了'}
];
return (
<div className='MessageBox'>
{commentArr}
</div>
)原因: render 函数中,不支持对象,或者数组中包括对象 解决方法: 不要使用整个对象,可以使用对象中的某个键 let commentArr = [
{"authorName":'张三','date':'2008-8-8','message':'我要发财了'},
{"authorName":'李四','date':'2021-8-8','message':'我要起飞了'},
{"authorName":'王五','date':'2021-6-6','message':'我要走运了'}, {"authorName":'赵六','date':'2021-9-9','message':'我要脱单了'}
];
return (
<div className='MessageBox'>
{commentArr}
</div>
) |
|
|