最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to pass props to "css" element of styled components? - Stack Overflow

programmeradmin3浏览0评论

I can do this with a styled.div

styled.div`
    color: ${props=> props.color || 'black'};
`

How can I do something similar with a styled components css element?

const BlackBGCSS = css`
    color: ${props=> props.color || 'black'};
`

For now my solution is to create a factory function

const BlackBGCSS = (props)=> css`
    color: ${props=> props.color || 'black'};
`

I can do this with a styled.div

styled.div`
    color: ${props=> props.color || 'black'};
`

How can I do something similar with a styled components css element?

const BlackBGCSS = css`
    color: ${props=> props.color || 'black'};
`

For now my solution is to create a factory function

const BlackBGCSS = (props)=> css`
    color: ${props=> props.color || 'black'};
`
Share Improve this question asked Nov 22, 2018 at 14:06 Jad SJad S 2,9956 gold badges33 silver badges49 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

Kind of how you have it:

const myCSS = css`
    background: ${({ myColor }) => myColor || `black`};
`;

const MyComponent = styled('div')`
    ${myCSS};
`;

Then

<MyComponent myColor="red">Hello World</MyComponent>

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论