reactjs - props 使用 styled-components 作为 html 元素中的属性可见
问题描述
样式组件的高度和宽度关键字是什么?
VSpace.tsx
type VSpaceProps = { readonly height?: string; };
export const VSpace = styled.div`
height: ${({ height }: VSpaceProps) => height};
width: 100%;
`;
VSpace.defaultProps = { height: '.5rem' };
索引.tsx:
function App() {
return (
<VSpace height="0.4rem" />
);
}
输出:
<div height="0.4rem" class="VSpace-sc-18gziyv-1 kkKpPd"></div>
解决方案
不,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 深入了解。
推荐阅读
- python - 在列表中的数据框中查找日期,按 ID 分组
- javascript - 如何使用下面的 HTML 结构创建悬停时出现的叠加层?
- python - 展平 json 对象
- python - Glob没有获取文件?
- c# - 将 SSL 证书与 Fleck Websockets 一起使用
- reactjs - 计数器编号不变(React JS)
- html - 为什么在包含内联元素的块容器的行高中添加了额外的 2px?
- tensorflow - 尝试导入 tensorflow GPU 时出错
- scheme - 是否有 foldr 的应用程序,其中结果表达式的计算结果是原始列表的相同副本?
- spring-boot - 如何修复 SessionFactory Spring MVC 错误