【问题标题】:props are visible as attributes in html element using styled-componentsprops 使用 styled-components 作为 html 元素中的属性可见
【发布时间】:2020-06-22 04:52:50
【问题描述】:

样式组件是 heightwidth 关键字吗?

VSpace.tsx

type VSpaceProps = { readonly height?: string; };

export const VSpace = styled.div`
  height: ${({ height }: VSpaceProps) => height};
  width: 100%;
`;

VSpace.defaultProps = { height: '.5rem' };

index.tsx:

function App() {
  return (
    <VSpace height="0.4rem" />
  );
}

输出:

<div height="0.4rem" class="VSpace-sc-18gziyv-1 kkKpPd"></div>

【问题讨论】:

    标签: reactjs styled-components


    【解决方案1】:

    不,height 是可见的,因为它是 JSX 属性之一,它与样式组件无关。

    // height is a JSX.attribute type of div element
    function App() {
      return (
        <div height="0.4rem" />
      );
    }
    

    你可以查看它的TS类型:

    Type '{ height: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.
    

    JSX in Depth

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2019-02-18
      • 2021-09-22
      • 2018-04-15
      • 2021-09-19
      • 2020-06-07
      • 2020-05-28
      • 2020-02-15
      • 2020-03-02
      相关资源
      最近更新 更多