Extending a styled component
See original GitHub issueI have a question about the intended inheritance pattern for StyledComponents.
Say I have:
class MenuItem extends React.Component {
render() { return <foo className={this.props.className} />; }
}
const StyledMenuItem = styled(MenuItem)`
height: 10px;
`
I’d like to extend StyledMenuItem with a subclass, let’s say Settings
class Settings extends StyledMenuItem {
render() { return <bar className={this.props.className} />; }
}
const StyledSettings = styled(Settings)`
width: 10px;
`
But this pattern doesn’t seem to be functional, the render method of MenuItem is getting called rather than Settings. What’s the best practice for extending React methods as well as styling? Right now it seems like I need to do it in two steps, extending MenuItem to Settings, and then applying the MenuItem and Settings styles.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to extend styled components? - Stack Overflow
You can just do like this: const FlexContainer = styled.div` display: flex; flex-direction: column; justify-content: flex-start; ...
Read more >Basics - styled-components
styled -components utilises tagged template literals to style your components. It removes the mapping between components and styles. This means that when you're ......
Read more >Extend Styling of Existing or Third Party React Component ...
If you wish to style your own existing (or any third-party) React component, then you can simply use the styled-components' styled() ...
Read more >Start learn styled components!(2)-Extending Styles
You can through extending styles to finish same thing above and more beautiful, easier to maintain. The way to using extending styles is...
Read more >How To Add Styles To React Components With Styled ...
As a React Front-End developer, you sometimes need to take an existing React component and apply extra styles to it.
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 FreeTop 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
Top GitHub Comments
Inheritance isn’t a good pattern in React. Read more about that here: https://facebook.github.io/react/docs/composition-vs-inheritance.html
Personally I would rewrite your solution like this:
Hopefully that’s what you want to accomplish 😉
That’s helpful feedback, thanks for the advice. Time to do a little bit of refactoring