javascript - 从 chrome 开发工具获取 CSS 作为 JavaScript 对象值

标签 javascript css reactjs material-ui

我们刚刚开始使用 makeStyles 在我们的 React 组件中编写样式来自 Material-ui 的钩子(Hook)。

所以现在的 css 不是常规的 css 而是 js 对象 - 例如看起来像这样:

const useStyles = makeStyles(() =>
 createStyles({
     root: {
         display: 'flex'
     },
     divider:{
         width:'1px',
         backgroundColor:'rgba(44, 66, 107, 0.15)'
     }
 }),
);

不,之前 - 当我们使用 scss 文件时,有时我会“玩弄”chrome 开发工具上的样式:

enter image description here 当没问题时,我将其复制->粘贴到 scss 文件中。

当我尝试在这里做同样的事情时,我得到了在 js 对象中无效的常规 CSS: enter image description here

并且需要手动更改它才能作为 js 对象值有效: enter image description here

(在这个示例中,手动更改很少,但在其他示例中,可能需要进行很多更改...

寻找一种方法从 chrome 开发工具获取样式作为“JS”CSS 样式,而不是像现在这样的“常规”CSS。 当然,我可以使用一些手动解决方案,例如这个很棒的解决方案:

https://css2js.dotenv.dev/

但我想避免所有这些复制粘贴

谢谢。

最佳答案

使用styled-components

allows you to write actual CSS code to style your components.

import styled from 'styled-components';

const Button = styled.button`
  color: grey;
`;
<小时/>

引用:MUI官方文档建议:https://material-ui.com/guides/interoperability/#styled-components

关于javascript - 从 chrome 开发工具获取 CSS 作为 JavaScript 对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61041401/

相关文章:

html - 如何使元素重叠到另一个div

html - IE11 - 如何在禁用的字段集中启用滚动条

javascript - 有没有办法在 "React Select"中搜索选项时包含组标签

javascript - JavaScript 如何识别事件对象变量?

javascript - 每次单击按钮时将参数保存在数组中

css - 为什么我不能用 Angular5 显示 Font-Awesome

javascript - ReactJS:未捕获类型错误:无法读取未定义的属性 'setState'

javascript - setState 使用 File onLoad

javascript - Firefox 与 IE/Chrome 中的事件处理程序

javascript - 在 JavaScript 中用 "null"初始化变量是一种好习惯吗