【发布时间】:2021-03-04 16:49:09
【问题描述】:
我有一个样式组件,如下所示:
interface BoxProps {
color?: string;
backgroundColor?: string;
borderColor?: string;
}
export const Box = styled.div<BoxProps>`
position: relative;
padding: 0.75rem 1.25rem;
margin-bottom: 1rem;
border: 1px solid transparent;
border-radius: 0.25rem;
text-align: left;
color: ${(props) => props.color};
background-color: ${(props) => props.backgroundColor};
border-color: ${(props) => props.borderColor};
`;
它被包裹在另一个组件中:
export const Container: React.FC<ContainerProps> = ({
variant,
children,
...props
}) => {
const { color, backgroundColor, borderColor } = variantColor(variant);
return (
<div>
<Box
color={color}
backgroundColor={backgroundColor}
borderColor={borderColor}
{...props}
>
{children}
</Box>
<p></p>
// ...
</div>
);
};
如果我将StyledComponent<"div", any, BoxProps, never> 添加到React.FC<ContainerProps>:
React.FC<ContainerProps & StyledComponent<"div", any, BoxProps, never>>
传播道具会给我以下错误:
Rest types may only be created from object types
我试过React.HTMLAttributes<{}> & typeof Box.propTypes 和React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> 没有运气...
有没有办法将样式组件道具与父道具合并?
谢谢!
【问题讨论】:
标签: reactjs typescript styled-components