【发布时间】:2020-08-12 18:10:19
【问题描述】:
样式化组件:
import { Typography } from '@material-ui/core';
const Text = styled(Typography)<TextProps>`
margin-bottom: 10px;
color: ${({ textColor }) => textColor ?? textColor};
font-size: ${({ textSize }) => (textSize ? textSize + 'px' : '16px')};
`;
在组件内部使用:
<GlobalStyled.Text textColor="green" textSize="20">test</GlobalStyled.Text>
“警告:React 无法识别 DOM 元素上的 textColor 属性。如果您故意希望它作为自定义属性出现在 DOM 中,请将其拼写为小写 textcolor。如果您不小心从父组件,将其从 DOM 元素中移除。”
props 被传递给 Typography 组件本身,而不仅仅是 styled-component,如何解决这个问题?
更新
5.1.0 发布的样式化组件:https://github.com/styled-components/styled-components/releases/tag/v5.1.0
现在有新的瞬态道具,通过道具过滤解决了这个问题。您可以在道具名称前使用$propsName,这是一个美元符号,它将仅传递给样式组件!
【问题讨论】:
-
错字 -
color: ${({ textColor }) => textColor && textColor};甚至只是color: ${({ textColor }) => textColor}; -
谢谢 - 但是,仍然 - 在这里使用它有什么意义?就像你使用
${({ textColor }) => textColor}
标签: javascript reactjs typescript material-ui styled-components