Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to override child styles in styled-components

I have a Component and I want to override it's styles. I have used styled-components to build them.

My doubt is, how can one override styles of a child component.

I want to know better approach.

Example

 const Child = styles.div'
      font-size: '10px'
    '
    const Wrapper = styles.div'
      color: red
    '
const DummyComponent = () => (
  <Wrapper>
    <Child>Hello</Child>
  </Wrapper>
)

I want to change padding or margin or any other property of child.

What is the better approach. Using inline style or className. Or is there any better approach in styled-component to do this.

Using inline style

const DummyComponent = ({childStyles}) => (
  <Wrapper>
    <Child style={{...childStyles}}>Hello</Child>
  </Wrapper>
)

Using className

const DummyComponent = ({childClass}) => (
   <Wrapper>
       <Child className={childClass}>Hello</Child>
    </Wrapper>
)
like image 470
Kodanda Rama Durgarao Poluri Avatar asked Nov 24 '25 08:11

Kodanda Rama Durgarao Poluri


1 Answers

I would recommend to use classNames to override the behaviors. you just need your rules stronger. tex:

const DummyComponent = ({childClass}) => (
   <Wrapper className="div--wrapper">
       <Child className="div--child">Hello</Child>
    </Wrapper>
)
// css
.div--wrapper .div--child{
  // override css go here
}
like image 134
duc mai Avatar answered Nov 26 '25 03:11

duc mai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!