reactjs - 防止 React 显示用于样式化组件的道具?
问题描述
我Styled-Components
用React
.
我的代码如下所示:
const StyledLink = styled.a<StyledProps>`
font-size: ${(props) => pxToRem(props.fontSize!)};
//disabled state
${(props) =>
props.disabled &&
css`
color: grey;
pointer-events: none;
`}
`
const Link = ({href, disabled, fontSize = 16, children}: Props) => {
return(
<StyledLink href={href} fontSize={fontSize} disabled={disabled}>{children}</StyledLink>
)
}
一切正常,但是当我检查我的元素时,我看到:
<a href="#" font-size="16" disabled>Some Link</a>
我想要fontSize
并且disabled
成为仅针对目标的道具Styled-Components
。我不希望他们申请React
元素。有没有更好的方法来做到这一点,而无需将道具名称更改为类似styledFontSize
and styledDisabled
?
解决方案
styled-componentstransient props
为那些想要避免属性传递到属性名称应该具有$
(美元符号)前缀的 DOM 或 React 节点的人提供。
使用$
瞬态道具:
const StyledLink = styled.a<StyledProps>`
font-size: ${(props) => pxToRem(props.$fontSize!)};
//disabled state
${(props) =>
props.$disabled &&
css`
color: grey;
pointer-events: none;
`}
`
const Link = ({href, disabled, fontSize = 16, children}: Props) => {
return(
<StyledLink href={href} $fontSize={fontSize} $disabled={disabled}>{children}</StyledLink>
)
}
推荐阅读
- powershell - 如果文件名已经存在,则替换文件名的一部分
- python - 如何在 PyTorch 中执行元素产品?
- php - 如何检查sql的第一列或第二列中是否存在一个特定值?
- python - 类函数输出正确但方法错误
- swiftui - SwiftUI 如何使表单中的文本居中?
- mysql - 插入查询因错误重复条目而失败,其中选择查询返回 null
- regex - 我可以使用 IIS7 将流量从子域重定向到我的 mac 服务器吗
- python - 读取文件时排除空格
- tensorflow - Tensorflow Fit 以代码 1 退出,没有任何错误消息
- r - 如何将格式为“1/16/2019 1:24:51”的字符串解析为 POSIXct 或其他日期变量?