Created
May 6, 2016 11:49
-
-
Save mbrochh/188e9b14713d4100fc7e90d455ff7b85 to your computer and use it in GitHub Desktop.
ReactJS propTypes composition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Component1 extends React.Component { | |
static propTypes = { | |
some: React.PropTypes.bool, | |
prop: React.PropTypes.string, | |
types: React.PropTypes.object, | |
} | |
} | |
class Component2 extends React.Component { | |
static propTypes = { | |
additional: React.PropTypes.object.isRequired, | |
some: React.PropTypes.bool, | |
prop: React.PropTypes.string, | |
types: React.PropTypes.object, | |
} | |
render() { | |
let { additional, ...other } = this.props | |
return ( | |
<Component1 {...other} /> | |
) | |
} | |
} |
@erikras see, I'm still a JS syntax noob! That thing with the ...Component1.propTypes
is EXACTLY what I was hoping for! This is perfect, thank you so much!
And I agree that Redux helps with propTypes-hell, just in my special usecase, I'm dealing with props that don't make sense to be stored in a reducer, so that's why I was asking.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prop distribution hell is one of the things that Redux addresses. Pre-Redux, your root component needed all the data, and then each subsequent component level needed some subset of the data. With leaf nodes (or nodes closer to the leaves) being able to
connect()
to the data using Redux, a lot of that complexity is reduced.However, you have
Component1
'spropTypes
statically available to you, so why not something like this?