React 错误:样式道具值必须是对象 react/style-prop-object

新手上路,请多包涵

我正在尝试将文本环绕在图像周围,如果我使用以下代码:

 <div className="container">
         <img src={myImageSource} alt="swimmer" height="300" width="300" style="float: left" />
         <p> This is where the other text goes about the swimmer</p>
 </div>

现在我知道这 style="float: left" 是html 5。但是,如果我使用以下代码:

 <div className="container">
         <img src={myImageSource} alt="swimmer" height="300" width="300" align="left" />
            <p> This is where the other text goes about the swimmer</p>
 </div>

有用!为什么我不能在 React 中使用样式?

原文由 AltBrian 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 788
2 个回答

您仍然可以在反应中使用样式。试试: style={{float: 'left'}}

原文由 Shivam Gupta 发布,翻译遵循 CC BY-SA 4.0 许可协议

问题是您将样式作为 String 而不是 Object 。 React 希望您以对象表示法传递样式:

 style={{ float:`left` }}  // Object literal notation

或者另一种方式是:

 const divStyle = {
  margin: '40px',
  border: '5px solid pink'
};

<div style={divStyle}>   // Passing style as an object

有关 详细信息,请参阅文档

原文由 Akrion 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题