Published on

How to Override React Component Styles with Styled-Components

Authors
  • Name
    Ripal & Zalak
    Twitter

How to Override React Component Styles with Styled-Components

Styled-components is a popular CSS-in-JS library for styling React applications. However, overriding styles of existing components, especially custom React components, can sometimes be tricky. This guide explores different ways to correctly override styles using styled-components.

The Problem

When using styled() on a custom React component, styles may not be applied correctly. Consider the following example:

import React, { Component } from 'react'
import styled from 'styled-components'

export default class MyLabel extends Component {
  render() {
    return <label>{this.props.children}</label>
  }
}

const StyledMyLabel = styled(MyLabel)`
  color: green;
`

Using <StyledMyLabel>My Styled Label</StyledMyLabel> may not change the color because styled-components styles are applied using class names, but the MyLabel component does not forward the className prop to the label element.

Solution: Forwarding the className Prop

To ensure that styles are applied, the className prop must be passed to the correct element inside the component:

import React, { Component } from 'react'
import styled from 'styled-components'

export default class MyLabel extends Component {
  render() {
    return <label className={this.props.className}>{this.props.children}</label>
  }
}

const StyledMyLabel = styled(MyLabel)`
  color: green;
`

Now, when you use <StyledMyLabel>My Styled Label</StyledMyLabel>, the color will be applied correctly.

Alternative: Using style Prop

If styled-components is not a requirement, you can use the style prop to override styles directly:

<label style={{ color: 'green' }}>{this.props.children}</label>

Or by defining an object:

const style = { color: 'green' }
;<label style={style}>{this.props.children}</label>

Alternative: Using theme for Third-Party Components

Some third-party components accept a theme prop to override styles. Example:

import { AppBar } from 'react-toolbox/lib/app_bar'
import newTheme from './NewAppBar.css'

;<AppBar theme={newTheme} />

Key Takeaways

  • Always forward the className prop when wrapping a React component with styled-components.
  • Using the style prop is a quick alternative but may not be ideal for complex styles.
  • Some third-party components accept a theme prop to customize styles.

FAQs

Why doesn’t my styled-component override the styles of my React component?

Styled-components apply styles using class names. If your React component does not pass the className prop to the appropriate element, the styles won’t be applied.

Is using !important a good solution?

No, relying on !important is a bad practice as it makes styles harder to override later. Instead, ensure proper class application and cascading styles.

What if my component is from a third-party library?

If the component accepts a className prop, use styled-components. Otherwise, check if it supports a theme prop or extend its styles via global styles.