const Button = styled.button`
display: inline-block;
width: 300px;
background-color: black;
`
const ButtonHref = styled.a`
${Button}
` 所以我有两个样式化的组件。我想继承“Button”样式,但创建另一个标签。我使用react-emotion。我该怎么做呢?
发布于 2019-05-16 16:25:11
这里有几个选项,使用组合,样式组件,或使用道具。第二个选项可能就是您想要的,但我也提供了另外两个选项。
1.使用组合
const baseButton = css`
color: white;
background-color: black;
`
const fancyButton = css`
background-color: red;
`
render() {
return (
<div>
<button css={baseButton}></button>
<button css={[baseButton, fancyButton]}></button>
</div>
)
}第二个按钮将具有baseButton和specialButton样式。
或者..。
const baseButton = css`
color: white;
background-color: black;
`
const fancyButton = css`
${baseButton};
background-color: red;
`
render() {
return (
<div>
<button css={baseButton}></button>
<button css={fancyButton}></button>
</div>
)
}2.使用样式化组件
const Button = styled.button`
color: white;
background-color: black;
`
const Fancy = styled(Button)`
background-color: red;
`
render() {
return (
<div>
<Button>Button</Button>
<Fancy>Fancy</Fancy>
</div>
)
}这适用于任何接受className属性的组件,button就是这样做的。
3.使用 props
const Button = styled.button`
color: white;
background-color: ${props => props.fancy ? 'red' : 'black'};
`
render() {
return (
<div>
<Button>Button</Button>
<Button fancy>Fancy</Button>
</div>
)
)发布于 2020-09-05 13:34:12
如果您只想要一个具有与Button完全相同的样式的a,那么可以使用<Button as=“a” />
https://stackoverflow.com/questions/55916786
复制相似问题