PropTypes of wrapped component leak through
See original GitHub issueSuppose 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:
- Make
age
anddispatch
non required or - Render
<Foobar/>
resp.<Connect(Foobar)/>
with dummy values forage
anddispatch
.
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:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top 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 >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
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
It might be worth documenting this in the react-redux docs though.