question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using withComponent('tag') on styled(Component) removes styling

See original GitHub issue

Version

2.2.3

Reproduction

codesandbox: https://codesandbox.io/s/2xwzowxx3j

const BaseComponent = styled.div`
  background: black;
  padding: 10px;
  color: red;
`

export const ComponentFromDifferentFile = styled(BaseComponent)`
  margin: 5px;
`.withComponent('p')

export const ComponentFromSameFile = BaseComponent.extend`
  margin: 5px;
`.withComponent('p')

Expected Behavior

I would assume that both ComponentFromDifferentFile, ComponentFromSameFile should still have the styling from BaseComponent.

Actual Behavior

ComponentFromDifferentFile does not have the style from BaseComponent. This only happens if I use withComponent.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
petergomboscommented, Nov 16, 2017

@philpl Thanks for the quick reply! Could you perhaps suggest a way I should approach this then? How can I change a styled(component)'s html tag?

2reactions
kittencommented, Nov 15, 2017

When you call styled(BaseComponent) it doesn’t extend the BaseComponent but instead wraps it in another styled component.

So calling withComponent on the outer styled component will replace BaseComponent with "p" instead of replacing "div" in the child styled component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basics - styled-components
styled -components utilises tagged template literals to style your components. It removes the mapping between components and styles.
Read more >
How to achieve tag agnostic styled components?
withComponent together with .extend to both change the tag and use additional styles ... import styled from 'styled-components' const styles = ` color:...
Read more >
How To Use Styled-Components In React - Smashing Magazine
If you're looking for various options in styling React components, ... Styled components remove unused styles, even if they're declared in ...
Read more >
Styled Components Flashcards - Quizlet
It removes the mapping between components and styles. This means that when you're ... withComponent to extend the styles and use a different...
Read more >
How to style React Router links with styled-components
Using styled -components provides a unique className for your styles and eliminates the problems associated with className duplications, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found