Extending styles with styled-components not working

后端 未结 2 1003
礼貌的吻别
礼貌的吻别 2021-02-19 10:59

I\'m trying to extend styles for a react component using styled-components but is not working. AFAIK, I\'m doing it the right way, but perhaps I\'m missing somethin

相关标签:
2条回答
  • 2021-02-19 11:46

    As stated in documentation:

    The styled method works perfectly on all of your own or any third-party components, as long as they attach the passed className prop to a DOM element.

    Example

    // This could be react-router-dom's Link for example, or any custom component
    const Link = ({ className, children }) => (
      <a className={className}>
        {children}
      </a>
    );
    
    const StyledLink = styled(Link)`
      color: palevioletred;
      font-weight: bold;
    `;
    
    render(
      <div>
        <Link>Unstyled, boring Link</Link>
        <br />
        <StyledLink>Styled, exciting Link</StyledLink>
      </div>
    );
    

    Ref: https://www.styled-components.com/docs/basics#styling-any-component

    0 讨论(0)
  • 2021-02-19 11:53

    I didn't know that was a way to do it. I would do:

        const Link = styled.a`
        ..put you css styles here (className styles)
       `
    
       const StyledLink = styled(Link) `
          color: palevioletred;
          font-weight: bold;
       `
    
    render(){
    return(
    <div>
        <Link>Unstyled, boring Link</Link>
        <br />
        <StyledLink>Styled, exciting Link</StyledLink>
      </div>
    )
    }
    
    0 讨论(0)
提交回复
热议问题