javascript - Styled Components / React - 外部元素的样式
问题描述
我正在使用Material UI组件和MaterialTable,我想使用StyledComponents对组件进行时尚化
但是当我尝试使用 StyledComponents 应用样式时,我没有得到想要的结果
例子:
import styled from 'styled-components'
import MaterialTable from "material-table";
export const MaterialTableStyled = styled(MaterialTable)`
/* STYLE FOR FILTER ROW */
tbody > .MuiTableRow-root:first-child {
background-color: #F1F3F4 !important;
}
`
当我使用MaterialTableStyled
组件时没有应用样式。
相反,如果我div
在 StyledComponent 上使用 a:
import styled from 'styled-components'
export const MaterialTableStyled = styled.div`
/* STYLE FOR FILTER ROW */
tbody > .MuiTableRow-root:first-child {
background-color: #F1F3F4 !important;
}
`
我的自定义样式以这种方式完美地工作:
<MaterialTableStyled> // DIV WITH STYLES
<MaterialTable
// ALL PROPS AND STUFFS
/>
</MaterialTableStyled>
...问题是:有可能“覆盖”或更改样式而不使用div
更改样式?
解决方案
组件必须将 className 属性传递给它们的子组件才能使样式化功能起作用。
快速浏览一下,MaterialTable 并没有这样做,因此样式化的组件无法将另一个 css 类分配给表。
与样式化功能兼容的组件
const StyledFriendlyComp = ({className}) => <div className={className}>Styles being applied</div>
不适用于样式化函数的组件
const StyledFriendlyComp = () => <div>Styles not working</div>
在这种情况下,您需要使用另一个元素(如 div)来包装组件。
推荐阅读
- java - 在 Java 中为变量分配新值
- raspberry-pi - 使用 Raspberry Pi 和 JTAG HS3 电缆对 CPLD 进行编程
- charts - 当 2nd pods bootstraps / 添加到副本集时,helm chart "stable/mongodb-replicaset" 卡住了
- c++ - 我想让 setmousecallback 函数在仍然按下鼠标左键时连续传递坐标
- sql - 圆形 GETDATE (SQL Server)
- python - 在优化函数 scipy 中动态创建变量
- flowtype - 在 Flow 中展平对象属性
- android - ConstraintLayout 占位符视图不会覆盖尺寸
- php - Elasticsearch 距离查询异常抛出
- google-apps-script - 将活动单元格复制到包含字符串的其他单元格