javascript - React 不接受 CSS 类
问题描述
我遇到了一个简单的错误,但找不到它。
我创建了一个常量,也为一个元素设置样式。现在我想将常量放入 CSS 中,但不知何故这不起作用。
我想删除这一行:
<div style={OVERLAY_STYLES}/>
有人可以解释我的错误吗?
const OVERLAY_STYLES = {
position: "fixed",
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: "rgba(0,0,0, .7)",
zIndex: 1000,
};
export default function Modal({ open, children, onClick }) {
const [check, setCheck] = useState([]);
......
return ReactDOM.createPortal(
<div className="OVERLAY_STYLES">
<div style={OVERLAY_STYLES}/>
<div className="MODAL_STYLES">
<h1>Blabla.</h1>
{attribute.map((att, index) => {
return (
<div>
......
</div>
);
})}
<div className="b01">
<button onClick={() => onClick(check)} >Submit</button>
</div>
</div>
</div>
,
document.getElementById("portal")
);
}
.OVERLAY_STYLES {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0, .7);
z-index: 1000;
};
解决方案
在反应内联样式中,所有值都需要在 ' ' 内。OVERLAY_STYLES
将您的代码更改为:
.OVERLAY_STYLES {
position: 'fixed';
top: '0';
left: '0';
right: '0';
bottom: '0';
backgroundColor: 'rgba(0,0,0, .7)';
z-index: '1000';
};
另外,没有破折号,所以background-color
改为backgroundColor
推荐阅读
- asp.net - IIS 上的随机 500 错误
- reporting-services - 在 SSRS 中的查找中格式化字段
- dynamics-crm - CRM 2013 重新排序站点地图功能区中的实体
- .net-core - dotnet core webapi调用.net webapi2
- hash - 将 CRC32 值理解为除法余数
- docker - 在 macOS 上运行时,如何让 GitLab Runner 停止像 Linux 一样运行?
- json - 如何定义模式任意 JSON 对象?
- python-3.x - pandas 基于分布的系列抽样
- android - Android 和 iOS 中的 Nativescript 并发和低延迟音频
- javascript - 如何获取选定复选框的标签