Skip to content

Instantly share code, notes, and snippets.

@solomonhawk
Last active Oct 29, 2015
Embed
What would you like to do?
// yell at any developers using `this.props.whatever` if PropTypes.whatever is not set
let PropCheckMixin = {
componentWillMount() {
this.validateProps(this.props)
},
componentWillReceiveProps(nextProps) {
this.validateProps(nextProps)
},
validateProps(props) {
let { displayName, propTypes } = this.constructor
for (let prop in props) {
if (!propTypes[prop]) {
console.warn(`You set a property "${prop}" on Component "${displayName}" but did not provide a PropType declaration for this prop.`)
}
}
}
}
module.exports = PropCheckMixin
@skogsmaskin
Copy link

skogsmaskin commented May 8, 2015

This is really helpful! Tnx!

@noscripter
Copy link

noscripter commented Jun 3, 2015

this seems helpful, refer http://viget.com/extend/check-your-props

@gajus
Copy link

gajus commented Aug 30, 2015

I have developed a higher order component that does this.

https://github.com/gajus/react-strict-prop-types

It is more or less the same as this mixin. The source code is written in ES6 and uses ES6 classes. It also adds support for HTML properties using allowHTMLProps option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment