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.

Default props behavior changed between v3 and v4

See original GitHub issue

The behavior of how defaultProps are handled has changed between s-c v3 and v4, I’m not exactly sure if this change was intentional but it was not apparent in the changelog, so that’s why I’m filing it 😃.

Anyways, in v3 I could have a Text component with some default props such as the font. I could then create a Heading component which is based on Text but may provide some different default props. In v3, the following example works fine and both the Heading and Text components always have a default font prop. In v4 however, the defaultProps of the Heading overwrites all the default props of Text. If I don’t define any defaultProps for Heading it seems to work fine.

const Text = styled.span`
  font-family: ${({ font }) => {
    if (!font) console.error('FONT PROP NOT FOUND!')
    return font
  }};
`

Text.defaultProps = {
  font: 'sans-serif'
}

const Heading = styled(Text)`
  display: block;
  font-size: 38px;
`

Heading.defaultProps = {}

Environment

npx envinfo --system --binaries --npmPackages styled-components,babel-plugin-styled-components --markdown --clipboard

N/A, see reproduction.

Reproduction

https://www.webpackbin.com/bins/-KeeZCr0xKfutOfOujxN https://codesandbox.io/s/rkmNRByE4

The exact same demo in two different versions of Styled Components:

s-c v3, Heading and Text are both using sans-serif: https://codesandbox.io/s/6wmwk9l8kk s-c v4, the font-family for Heading is undefined because the font prop is undefined: https://codesandbox.io/s/9jlw0zq5yo

Steps to reproduce

Expected Behavior

The Heading component should inherit the defaultProps from the Text component like it did in v3. I think in v3 the defaultProps would be merged?

image

Actual Behavior

The defaultProps of Heading overwrites the defaultProps of the Text component even though Heading is based on the Text component.

image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
probablyupcommented, Jun 5, 2019

Released in v4.3.0

3reactions
oomathiascommented, Mar 19, 2019

I’ve made a PR to close this issue https://github.com/styled-components/styled-components/pull/2260 and bring back defaultProps folding. I’m still waiting for feedback, but IMO it LGTM.

ps: They are not being defined at built time, even in React. ps2: I was replying to a comment, which got deleted 🤷‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Styled Components, required props defined as defaultProps ...
It behavior still not same as when didn't use styled-component. all props changes to optional type while i need some props is required....
Read more >
Migration from v3 to v4 - Material UI - MUI
This is a reference for upgrading your site from Material UI v3 to v4. ... preventDefault() is meant to stop default behaviors like...
Read more >
HOC - Apollo GraphQL Docs
props can contain the logic to provide exactly the props your pure component needs from the data provided by your GraphQL API. Example....
Read more >
React-admin - Upgrading to v4 - Marmelab
React-admin v4 uses react-router v6, which changes quite a few things internally. ... We removed the undoable prop as we introduced the mutationMode...
Read more >
React: Everything about Default Props | by Chidume Nnamdi
defaultProps is a property in React component used to set default values for the props argument. It will be changed if the prop...
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