React render:对象作为React子对象无效

React render:对象作为React子对象无效,第1张

React render:对象作为React子对象无效

问题是因为你回来了

return (        {items}     )

这是的等效

return ({items: items})
即 您将返回具有键
items
和的对象
React doesn't expectobjects for rendering
。你可以写

   const items = items.map((i) =>{      return ( <h1>{i.title}</h1> )   });   return items;

要么

     return items.map((i) =>{        return ( <h1>{i.title}</h1> )     });

要么

  const items = items.map((i) =>{      return ( <h1>{i.title}</h1> )   });  return <React.Fragment>        {items}     </React.Fragment>

PS请注意,前两种方法将开始起作用

react v16.0.0 onwards
而后一种方法将
v16.2
开始起作用。

但是,如果您使用的是较低版本,则需要将return元素包装在类似

    return (        <div>{items}</div>     )


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4886493.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-11
下一篇2022-11-11

发表评论

登录后才能评论

评论列表(0条)

    保存