Regression with `withComponent` and `defaultProps`
See original GitHub issueThere is a regression between Styled Components 3.4.10 and 4.2.0 in which the behavior of defaultProps
changes when used in combination with withComponent
.
Steps to reproduce
The following code works in 3.4.10 and crashes in 4.2.0.
import React from "react";
import { render } from "react-dom";
import styled from 'styled-components';
const STYLES = {
fallback: {
color: 'red',
},
}
const A = styled.div`
color: ${({styling}) => STYLES[styling].color};
`
A.defaultProps = {
styling: 'fallback',
}
const B = ({...props}) => <div {...props} />
const C = styled(A.withComponent(B))``;
render((<C>text</C>), document.getElementById("root"));
Expected Behavior
The component to render text
in a red font.
https://codesandbox.io/s/0m1zp6k7pw
Actual Behavior
The execution crashes with error STYLES[styling] is undefined
. This is because styling
is undefined
instead of fallback
(as specified in the defaultProps
of A
), leading to an access of STYLES[undefined].color
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Typescript and defaultProps on a styled component
Based on your type definition for ButtonType , it is possible for intent to be undefined . Typescript compiler detects that and gives...
Read more >Releases - styled-components
Transient props are a new pattern to pass props that are explicitly consumed only by styled components and are not meant to be...
Read more >styled-components | Yarn - Package Manager
Added Comp.withComponent(Other) to allow cloning of an existing SC with a new tag. (see #814. ... Fix defaultProps used instead of ThemeProvider on...
Read more >UNPKG - @nateradebaugh/react-datetime
16, - Fix regression for selecting when date format is YYYY or YYYY-MM. ... 73, - Use static property `defaultProps` instead of `getDefaultProps`....
Read more >Hands-On way to build Frontend with React | Nick Ovchinnikov
Default props. ... You can be familiar with component library approach where you have full spec ... Yeah, hermeneutics, it could detect visual...
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
@konojunya This is an undocumented regression in version 4 of styled-components. A regression in the world of software describes the appearance of a bug which was absent in a previous revision.
I am aware that
withComponent
is a candidate for deprecation, it is not deprecated yet. Therefore it should work in v4 as it did in v3.Closing, feel free to make a documentation PR against the website!