简体   繁体   English

如何在样式组件中键入属性和样式道具 - React

[英]How to type attrs and styles props in styled components - React

Styles:款式:

interface BtnProps {
  variant: string;
  size: string;
  mr: number;
}

const Btn = styled(Button).attrs<BtnProps>(({ variant, size }) => ({
  forwardedAs: Link,
  size: size ?? 'md',
  variant: variant ?? 'primary'
}))<BtnProps>`
  color: white;
  margin-right: ${({ mr }) => mr ?? '10px'};
  &:hover {
    color: white;
  }
`;

TypeScript doesn't check type of mr variable, i can put whatever i want in it. TypeScript 不检查mr变量的类型,我可以在里面放任何我想要的东西。

Issue with my code was forwardedAs .我的代码问题被forwardedAs

Got an answer:得到了答案:

Yes when you render a custom component instead of a HTML primitive, eg "div" we don't filter props.是的,当您呈现自定义组件而不是 HTML 原语时,例如“div”,我们不会过滤道具。

source: https://github.com/styled-components/styled-components/issues/3039来源: https : //github.com/styled-components/styled-components/issues/3039

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM