Skip to content

Instantly share code, notes, and snippets.

@solomonhawk
Last active October 29, 2015 04:02
Show Gist options
  • Save solomonhawk/22303bf6197d4eb3ff43 to your computer and use it in GitHub Desktop.
Save solomonhawk/22303bf6197d4eb3ff43 to your computer and use it in GitHub Desktop.
// 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

This is really helpful! Tnx!

@noscripter
Copy link

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