conditional rendering in styled components

后端 未结 5 2214
失恋的感觉
失恋的感觉 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条回答
  •  猫巷女王i
    2020-12-07 15:05

    If your state is defined in your class component like this:

    class Card extends Component {
      state = {
        toggled: false
      };
      render(){
        return(
          
            I'm black text
            

    I will be rendered green

    ) } }

    Define your styled-component using a ternary operator based on that state

    const CardStyles = styled.div`
      p {
        color: ${props => (props.toggled ? "red" : "green")};
      }
    `
    

    it should render just the

    tag here as green.

    This is a very sass way of styling

提交回复
热议问题