reactjs - styled-component 生成自定义 css 类
问题描述
我有一个关于styled-components
图书馆的问题。我知道这可能不是一种惯用的做事方式,但我很好奇是否有可以返回 CSS 类名的 styled-component 方法。然后我可以将该类名传递给我渲染的组件。这样,我可以重用一些 CSS。再说一次,也许这违背了将隔离样式限定为单个组件的目的。
例如,是否有类似的功能generateCss
?
const Button = styled.div`
background-color: blue;
`
const myCssClassName = styled.generateCss`
background-color: blue;
`
...
<Button className={myCssClassName} />
解决方案
如果你想自定义一个现有的样式组件,你可以使用.extend()
API。
例如:
const Button = styled.div`
background-color: red;
`;
const BlueButton = Button.extend`
background-color: blue;
`;
export default () => (
<BlueButton />
);
推荐阅读
- sanity-check - 引用数组中的健全性查询
- c++ - 如何等待超时值
- powershell - 安装程序。检查文件夹中的文件然后终止进程
- java - JSP 选择代码输出完整的 html 源代码而不是实际值
- c# - MVC5 DropdlwonList:设置单个列的长度
- hibernate-envers - Hibernate Envers Audit Query - 仅检索所有实体的最新修订
- javascript - 阻止对快速路线的访问,直到路线完成
- highcharts - 在 requirejs 中引用 highstock.js 会引发错误:无法读取未定义的属性“stockChart”
- mysql - 如何将一个 Excel 表拆分为多个 MySQL 表?
- arrays - 在 React 中动态过滤掉 JSON 响应