conditional rendering in styled components

后端 未结 5 2206
失恋的感觉
失恋的感觉 2020-12-07 14:19

How can I use conditional rendering in styled-components to set my button class to active using styled-components in React?

In css I would do it similarly to this:<

5条回答
  •  爱一瞬间的悲伤
    2020-12-07 15:00

    I haven't seen this syntax, which I feel is the cleanest when you need to make a complete block conditional:

    const StyledButton = styled(button)`
        display: flex;
        background-color: white;
    
        ${props => !props.disabled} {
            &:hover {
                background-color: red;
            }
    
            &:active {
                background-color: blue;
            }
        }
    `;
    

    So there's no need to close/open ticks to get it working.

提交回复
热议问题