-
-
Save solomonhawk/22303bf6197d4eb3ff43 to your computer and use it in GitHub Desktop.
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
// 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 |
this seems helpful, refer http://viget.com/extend/check-your-props
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
This is really helpful! Tnx!