我已经创建了一个Text
组件,它接收了几个props。这些props被传递到样式化组件,在那里应用样式。我不想把props传递给DOM,我只想在样式化的组件中访问它们。
将props转换为transient props是可行的,但它会创建重复的代码,我认为这会使整个组件变得不必要的大。
我想知道,如何传递一些 prop 到我的风格组件,而不传递给DOM,而不必重新定义每个 prop 。我试过使用shouldForwardProps
,但我不能让它与Typescript一起工作。
type Props = {
children: React.ReactNode;
size: number;
height: number;
color: string;
};
const StyledText = styled.p<{ $color: string; $size: number; $height: number }>`
color: ${({ $color }) => $color};
font-size: ${({ $size }) => `${$size}px;`};
line-height: ${({ $height }) => `${$height}px;`};
...
`;
const Text: React.FC<Props & React.HTMLAttributes<HTMLParagraphElement>> = ({ children, size, height, color, ...rest }) => {
return (
<StyledText $size={size} $height={height} $color={color} {...rest}>
{children}
</StyledText>
);
};
2条答案
按热度按时间ou6hu8tu1#
这里是一个使用
shouldForwardProps
的解决方案,正如你所建议的,我相信这是你所要求的。我曾尝试将customAttributeNames
绑定到Props
,但我已经放弃了。这是一个sandbox,所以你可以看到它的工作。
qoefvg9y2#
您需要的是拥有一个默认的StyledText组件,然后使用
styled
函数覆盖该组件然后像这样覆盖它
通常在设计系统中,您定义文本/排版的不同变体。它是有限的(即:标题、副标题、文本)。因此,最好是有不同的组件,这样比影响最终用户传递变量,风险不符合您想象的设计