Using withComponent('tag') on styled(Component) removes styling
See original GitHub issueVersion
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:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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?
When you call
styled(BaseComponent)
it doesn’t extend theBaseComponent
but instead wraps it in another styled component.So calling
withComponent
on the outer styled component will replaceBaseComponent
with"p"
instead of replacing"div"
in the child styled component.