Styled Components
Sun April 3, 2022 10:05 AM
安装
1 | npm install --save styled-components |
开始
编写第一个组件
1 | const Title = styled.h1` |
基于属性渲染
1 | const Button = styled.button` |
继承样式
1 | // 相当于 styled('button') |
替换标签
1 | const Button = styled.button` |
替换自定义组件
1 | const Button = styled.button` |
传递属性
1 | const Input = styled.input` |
Note how the inputColor prop is not passed to the DOM, but type and defaultValue are. That is styled-components being smart enough to filter non-standard attributes automatically for you.
基于预处理器 stylis
1 | const Thing = styled.div` |
附加属性
1 | const Input = styled.input.attrs(props => ({ |
动画
1 | const rotate = keyframes` |
引用其他组件
1 | const Link = styled.a` |
This behaviour is only supported within the context of Styled Components: attempting to mount B in the following example will fail because component A is an instance of React.Component not a Styled Component.
1 | class A extends React.Component { |
对象式样式
1 | // Static object |
主题
1 | // Define our button, but with the use of props.theme this time |
问题
- 样式判断是在样式组件 style.js 中编写还是在组件中编写?
- 函数功能薄弱的问题如何解决?例如函数。