[英]styled-components with custom component not applying styles
我尝试了以下代码来设置由 react-id-swiper 实现的内部元素的样式: https://kidjp85.github.io/react-id-swiper/
import Swiper from 'react-id-swiper';
import styled from 'styled-components';
const MySwiper = ({ className, children }) => (
<Swiper className={className} {...params}>
{children}
</Swiper>
)
const StyledMySwiper = styled(MySwiper)`
.swiper-container{
background-color: red !important;
}
.swiper-slide{
background-color: red !important;
}
.swiper-wrapper{
background-color: red !important;
}
`;
然后在我的组件中使用它:
function CustomSwiper(props) {
return (
<StyledMySwiper>
<div>Slide #1</div>
<div>Slide #2</div>
<div>Slide #3</div>
<div>Slide #4</div>
<div>Slide #5</div>
</StyledMySwiper>
);
}
export default CustomSwiper;
但是 styles 没有应用。 我在这里做错了什么?
从StyledTestComponent
中删除.h1
,自定义 styles 将应用于TestComponent
。
const StyledTestComponent = styled(MyTestComponent)`
{
color: red;
font-size: 100px;
}
`;
如果TestComponent
有像下面这样的孩子,
function TestComponent({ className }) {
return (
<h1 className={className}>
<div className='child'> test div element</div> //child
aaaaasdfsdfsd
</h1>
);
}
您可以使用div
标签或className
来应用 styles,
const StyledTestComponent = styled(MyTestComponent)`
{
color: red;
font-size: 100px;
} // This will apply to the main container
.child {
color: green;
font-size: 100px;
} // This will apply to child element
`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.