【问题标题】:React - Objects are not valid as a React child?React - 对象作为 React 孩子无效?
【发布时间】:2020-06-12 00:09:39
【问题描述】:

我有一个演示 here

这只是一个带有背景图像的简单样式,但我收到了错误

Objects are not valid as a React child (found: object with keys {backgroundImage}). If you meant to render a collection of children, use an array instead.

这个错误是什么意思,不能用样式来做到这一点。有没有办法解决这个问题

【问题讨论】:

  • 异地demo也可以,但也请在问题中附上相关代码。

标签: reactjs


【解决方案1】:

您的style 属性需要在右括号内:

<div
  style={{
    backgroundImage: ...
  }}>
</div>

附加信息:

【讨论】:

    【解决方案2】:

    你有style作为div的后代生活

            <div>
              style={{
                  backgroundImage: 'url(https://dummyimage.com/600x400/000/fff)'
              }}
            </div>
    

    你需要这样做:

            <div
              style={{
                  backgroundImage: 'url(https://dummyimage.com/600x400/000/fff)'
              }}>
            </div>
    
    

    你有style住在外面&lt;div&gt;不在里面

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 2018-05-27
      • 2021-10-17
      • 2017-02-09
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多