doubleyong
管理员
管理员
  • 最后登录2024-04-24
  • 发帖数1167
  • 最爱沙发
  • 喜欢达人
  • 原创写手
  • 社区居民
  • 忠实会员
阅读:3197回复:0

[react]react 报错:Objects are not valid as a React child

楼主#
更多 发布于:2021-04-14 16:54
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>
        )
知识需要管理,知识需要分享
游客


返回顶部

公众号

公众号