Is it possible to extend a component and modify functional behavior while maintaining the component API?
See original GitHub issueBasically, I want to extend a components styles and include all the API behavior of a styled-component (as
prop, string interpolation, etc), but add some functional differences to the render function.
Example
Simple style-only extension works just fine:
const Button = styled.button`
border: 1px solid gray;
background-color: lightgray;
`;
const BlueButton = styled(Button)`
border-color: darkblue;
background-color: lightblue;
`;
I can use BlueButton
with an anchor tag using the as
prop: <BlueButton as="a">
.
What if I want to modify the render behavior as well?
let IconButton = ({ iconClass, children, ...props }) => (
<Button {...props}>
<i className={iconClass} /> {children}
</Button>
);
IconButton = styled(IconButton)`
i {
height: 30px;
width: 30px;
margin-right: 5px;
}
`;
The above captures the styling, but breaks when using the as
prop. I would love to still be able to do <IconButton as="a">
. I’m not sure if there is a pattern that will allow me to do this.
Live Example
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:18 (5 by maintainers)
Top Results From Across the Web
How to Override Existing React Components | Pluralsight
To override a style, you have two options. Either you can pass a style object, or you can pass a function that will...
Read more >How To Extend Classes with Angular Component Inheritance
Learn how to use the power of inheritance to extend your Angular components with common functionality.
Read more >How to extend / inherit components? - angular - Stack Overflow
Just use inheritance, extend parent class in child class and declare constructor with parent class parameter and this parameter use in ...
Read more >Reusable Components in React — A Practical Guide
How to design reusable React components and reuse them across projects and apps. Learn more about best practices with fragments, props, ...
Read more >React.Component
The render() method is the only required method in a class component. When called, it should examine this.props and this.state and return one...
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
Related feature request: #2129
Basically, we need a way to tell styled-components to ignore the
as
prop and just pass it on to the wrapped component.In your example, it would then be passed on to the
Button
component and everything would work as expected again. To visualise this here’s an example of the prop being passed down explicitly:Unfortunately, the request wasn’t received very well by the s-c team so far.
What are the drawbacks to following the same pattern that React did with refs?
It seems like we already have a well-established pattern for handling these types of props in React, why wouldn’t we just follow that pattern?