Lam*_*005 16 javascript emotion styled-components
const Button = styled.button`
display: inline-block;
width: 300px;
background-color: black;
`
const ButtonHref = styled.a`
${Button}
`
Run Code Online (Sandbox Code Playgroud)
所以我有两个样式组件。我想继承“按钮”样式但创建另一个标签。我使用反应情绪。我怎样才能做到这一点?
Bre*_*ody 24
这里有几个选项,使用组合、样式化组件或使用道具。第二个选项可能是您想要的,但我也提供了其他两个选项。
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>
)
}
Run Code Online (Sandbox Code Playgroud)
第二个按钮将具有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>
)
}
Run Code Online (Sandbox Code Playgroud)
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>
)
}
Run Code Online (Sandbox Code Playgroud)
这适用于任何接受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>
)
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8370 次 |
最近记录: |