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.

PropTypes of wrapped component leak through

See original GitHub issue

Suppose the following simple and common example:

import {connect} from 'react-redux'
import React, {Component, PropTypes} from 'react'

@connect((state) => ({
  age: state.age
}))
class Foobar extends Component {
  static propTypes = {
    name: PropTypes.string.isRequired,
    age: PropTypes.number.isRequired,
    dispatch: PropTypes.func.isRequired
  }

  render () {
    // ...
  }
}

So <Foobar/> needs name, age and dispatch whereas age and dispatch are provided via the @connect HOC. But when I render the component as one would exepect with

<Foobar name='My Name'/>

I get warnings from React, saying that Connect(Foobar) is missing the required props age and dispatch, even though <Foobar/> itself never get rendered without these props (so from <Foobar/>'s point of view everything is fine). To work around this I have two bad options:

  1. Make age and dispatch non required or
  2. Render <Foobar/> resp. <Connect(Foobar)/> with dummy values for age and dispatch.

The warning is strange by itself anyway, because looking at the code I see

Connect.propTypes = {
  store: storeShape
}

// ...

return hoistStatics(Connect, WrappedComponent)

And I also checked that the .propTypes of the returned HOC only contain the store props. Still I keep getting this warnings.

Btw, I am using the following versions:

  • react 15.2.1
  • react-redux 4.4.5

Thanks for your help.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
choffmeistercommented, Jul 14, 2016

FYI: There seems to be an ticket for the issue regarding these two babel plugins.

https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy/issues/17

0reactions
jimbollacommented, Jul 14, 2016

It might be worth documenting this in the react-redux docs though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript & React: Manipulating Prop Types | by Ross Bulat
Let's start by reviewing how to format and extend prop types. ... A wrapped component's types will therefore also be extended or modified...
Read more >
How to set PropTypes for the props in the HOC? #85 - GitHub
This is the code I tested and it works: const withLoading = (Component) => { const wrapped = ({ isLoading, ...rest }) =>...
Read more >
Accessing PropTypes via the main React package is deprecated
PropTypes is deprecated and has moved to another library. You should use npm install prop-types and use PropTypes from there. The code at...
Read more >
React and TypeScript | Cameron Little's Website
The most common cause of this is the higher-order component “stealing” properties from the wrapped component (or TypeScript thinks this is ...
Read more >
Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
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