【发布时间】:2021-01-13 12:00:08
【问题描述】:
我似乎在使用 StyledComponents 的组件中传播道具时遇到了麻烦。每当我尝试传递未在界面中定义的道具(例如样式标签)时,都会出现错误。 这是我当前的实现:
interface IParagraphProps {
text: string;
}
const StyledParagraph = styled.p`
max-width: 90%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
`;
const Paragraph = (props: IParagraphProps) => {
const { text, ...rest } = props;
return (
<StyledParagraph {...rest}>{text}</StyledParagraph>
)
};
export default Paragraph;
编辑:这是错误:Property 'style' does not exist on type 'IntrinsicAttributes & IParagraphProps'.
以及我使用这个组件的地方:
const Card = () => {
return (
<Paragraph
style={{ marginTop: "1rem" }}
text="whatever"
/>)
};
【问题讨论】:
-
您的错误发生在哪里?在这个组件内部还是在另一个调用这个组件的组件中?我没有这个组件的错误
-
另一个组件使用这个。示例:
-
能否提供调用代码?
-
用调用代码编辑了答案。
-
这是因为 Paragraph 函数接受 props 作为 IParagraphProps 类型并且没有任何
style的定义。在您的 Card 组件中,您将 style 设置为 Paragraph 的属性之一,该属性在其定义中不存在,即 IParagraphProps
标签: reactjs typescript styled-components