Skip to content

Instantly share code, notes, and snippets.

@danvk
Created September 17, 2014 16:52
Show Gist options
  • Save danvk/aeed640905698402cfb2 to your computer and use it in GitHub Desktop.
Save danvk/aeed640905698402cfb2 to your computer and use it in GitHub Desktop.
/**
* Mix this in to a component to check that all its props are also in propTypes.
* This uses the ES6 Proxy object, currently only available in Firefox or
* Chrome with special flags.
*/
var PropertyValidationMixin = {
getInitialState: function() {
if (typeof(Proxy) == 'undefined') {
console.warn('Proxy is not available in this browser; props will be unchecked.');
return;
}
// "this" here refers to the Component.
var component = this;
var rawProps = this.props;
var propTypes = this._descriptor.type.propTypes;
this.props = new Proxy(rawProps, {
get: function(obj, prop) {
if (prop == 'ref') {
// ... what is this?
} else {
if (!(prop in propTypes)) {
console.warn('Accessing property', prop, 'in', component,
'which is not declared in its propTypes.');
} else {
console.log('Validated', prop);
}
}
return rawProps[prop];
}
});
return {}; // Otherwise you can get Invariant Violation.
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment