gpt4 book ai didi

styled-components - 如何扩展样式组件?

转载 作者:行者123 更新时间:2023-12-04 13:25:39 25 4
gpt4 key购买 nike

我有一个样式组件:

interface FlexContainerProps {
children: any;
className?: string;
}

function FlexContainer(props: FlexContainerProps) {
const Container = styled.div`
display: flex;
flex-direction: column;
justify-content: flex-start;
`;
return (
<Container className={props.className}>
{props.children}
</Container>
);
}
当我在组件中使用它时,我希望能够扩展它。
以下不起作用,因为“扩展”类具有较低的特异性(或在代码中):
const FlexContainerExtended = styled(FlexContainer)`
flex-direction: column-reverse;
`;
以下工作但很hacky:
const FlexContainerExtended = styled(FlexContainer)`
flex-direction: column-reverse !important;
`;
还有另一种方法来扩展样式组件吗?

最佳答案

你可以这样做:

const FlexContainer = styled.div`
display: flex;
flex-direction: column;
justify-content: flex-start;
`;


const FlexContainerExtended = styled(FlexContainer)`
flex-direction: column-reverse;
`;

关于styled-components - 如何扩展样式组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52966623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com