【发布时间】:2020-05-12 09:27:36
【问题描述】:
使用styled-components 的新手总数。我想知道它的用途是什么?样式化后我应该如何实现组件生命周期方法?为简单起见,我删除了所有其他样式。
import styled from 'styled-components';
const Button = styled.button`
background-color: 'green'
`
export default Button;
我想知道如何进一步处理这个Button 组件?
传统上我们可以声明一个基于类的组件并实现一些生命周期方法,但现在有了这个styled-components,我不确定如何将它们组合在一起,因为它们实际上是单个Button 组件?
更新:
Button.js 的完整源代码。通过下面的代码,所有样式都将消失,我无法理解问题
import React from 'react';
import styled from 'styled-components';
// import Button from 'react-bootstrap/Button';
import color from '../config/color';
const Button = ({ children, onPress }) => (
<button type="button" onPress={onPress}>{children}</button>
);
const StyledButton = styled(Button)`
width: 12rem;
height: 54px;
font-size: 1rem;
background-color: ${(props) => {
if (props.inverted) return 'white';
if (props.disabled) return color.disabled;
return (props.color || color.primary);
}};
color: ${(props) => {
if (props.disabled) return color.disabledText;
if (props.inverted) return (props.color || color.primary);
return 'white';
}};
border:${(props) => (props.inverted ? `2px solid ${props.color || color.primary}` : 'none')};
border-radius: 60px;
&:hover {
filter: ${(props) => (props.inverted || props.disabled ? 'none' : 'brightness(95%)')}
}
`;
export default StyledButton;
【问题讨论】:
-
Hmmm.. 你想用这个组件中的样式按钮做什么?
-
@wentjun:我想我不太理解这个概念。传统上我会在
Button.js中定义Button组件。我正在考虑使用styled-components来设置样式? -
您可以像使用任何其他组件一样使用它..!让我写一个例子。
标签: javascript css reactjs styled-components