简体   繁体   English

Prop TypeScript React 组件错误,使用样式化组件

[英]Prop TypeScript Error With React Component, Using Styled-Components

I'm creating a Link component that has an option to change color to gray, using the disabled prop.我正在创建一个 Link 组件,该组件可以使用disabled道具将颜色更改为灰色。 When I hover over {({ disabled } , TypeScript is giving me a Property 'disabled' does not exist on type 'Pick<DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement> error.当我 hover over {({ disabled } , TypeScript 给我一个Property 'disabled' does not exist on type 'Pick<DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>错误。

Syntax looks correct, not sure what I'm missing here.语法看起来正确,不确定我在这里缺少什么。

const StyledLink = styled.a`

  color: blue;

  ${({ disabled }) => 
    disabled &&
    css`
      color: gray;
    `}
`;


export type TextLinkProps = {
  /**
   * text to be passed in as a link
   */
  link: string;
  /**
   * text to be rendered in the component.
   */
  children: React.ReactNode;
  /**
   * If `true`, color will be gray.
   */
  disabled?: boolean;
};

export function TextLink({ children, link, disabled = false }: TextLinkProps) {
  return (
    <StyledLink href={link} disabled={disabled}> 
      {children}
    </StyledLink>
  );
}

You should specify the props types you get in your styled component, as such:您应该指定您在样式化组件中获得的道具类型,如下所示:

const StyledLink = styled.a<{ disabled?: boolean }>``

Also just noticed, you can simplify you styles to omit css :还刚刚注意到,您可以简化 styles 以省略css

${({ disabled }) => disabled && "color: gray"};

TypeScript is right: anchor elements don't have attribute disabled . TypeScript 是对的:锚元素没有属性disabled

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a

暂无
暂无

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

相关问题 Typescript React/styled-components - 将 function 作为组件传递给组件会在缺少类型时引发错误 - Typescript React/ styled-components - passing function as prop to component throws error on missing type Typescript React/Nextjs styled-components - 将函数作为道具传递给组件会在缺少类型时引发错误 - Typescript React/Nextjs styled-components - passing function as prop to component throws error on missing type 将prop传递给样式组件的TypeScript错误 - TypeScript error passing prop to styled-components 使用带有打字稿的样式组件的“as”多态属性 - Using 'as' polymorphic prop of styled-components with typescript 使用样式组件的打字稿错误 - Typescript error using styled-components 当尝试使用 typescript 定义功能组件以与样式组件反应时,会出现“没有重载匹配此调用”的错误。 - when try to define a functional component in react with styled-components using typescript get error of “No overload matches this call.” 使用Material-UI组件的styled-components typescript错误 - styled-components typescript error with Material-UI component 使用样式组件和 Material UI 时的 Typescript 和 eslint 问题:`React 无法识别 DOM 元素上的 `showText` 属性 - Typescript and eslint issue when using styled-components and Material UI: `React does not recognize the `showText` prop on a DOM element` 使用 typescript 和 styled-components 在 React 应用程序中使用自定义字体 - Use custom font in React app using typescript and styled-components 如何将 forwardedAs prop 与样式组件一起使用? 将 forwardedAs prop 与 typescript 一起使用 - How to use forwardedAs prop with styled-components? Using forwardedAs prop with typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM