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.

Regression with `withComponent` and `defaultProps`

See original GitHub issue

There 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.

https://codesandbox.io/s/7j12406oyj

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
clentfortcommented, May 10, 2019

@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.

0reactions
probablyupcommented, Jan 2, 2020

Closing, feel free to make a documentation PR against the website!

Read more comments on GitHub >

github_iconTop 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 >

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