【发布时间】:2020-09-22 16:43:51
【问题描述】:
我使用样式化组件的时间很短。
目前我正在尝试对嵌套元素进行样式覆盖,但我无法理解我做错了什么。
所以我的结构是。
---------------Form.js----- ----------
import { FieldWrapper } from './FieldWrapper';
const Form = styled.form`
/** my form styles **/
`;
const StyledFieldWrapper = styled(FieldWrapper)`
/** my FieldWrapper styles **/
input {
/** input overrides **/
padding-bottom: 0.8rem;
height: 2rem;
line-height: 2rem;
box-sizing: content-box;
background-color: pink !important; // added just for visibility
}
`;
const MyForm = props => {
return (
<>
<Form>
<StyledFieldWrapper />
</Form>
</>
);
}
export { MyForm }
---------------FieldWrapper.js----- ----------
const Div = styled.div`
/** my div styles **/
`;
const Label = styled.label`
/** my label styles **/
`;
const Input = styled.input`
/** my input styles **/
`;
const FieldWrapper = props => {
return (
<Div>
<Label>
<Input />
</Label>
</Div>
);
}
export { FieldWrapper }
现在我期望发生的是FieldWrapper.js 中的样式将被Form.js 中的StyledFieldWrapper 元素覆盖,但这不会发生,我不知道为什么。我在过去和这个项目中都有过这样的覆盖。同样StyledFieldWrapper 不仅包含覆盖,它也有自己的风格,我什至看不到这些。
有人遇到过类似的问题吗?
注意:我尝试使用Styling Nested Components in Styled-Components 中的解决方案,但没有成功。
【问题讨论】: