With a higher order component, anything is possible:
const OnlyValidProps = WrappedComponent => {
return class extends React.Component {
render() {
const actualProps = Object.keys(this.props);
const expectedProps = Object.keys(WrappedComponent.propTypes);
const hasPropMisMatch = (
actualProps.length != expectedProps.length ||
!actualProps.every(key => expectedProps.contains(key))
);
if (hasPropMisMatch) {
console.warn(`Props mismatch! expected: ${expectedProps} actual: ${actualProps}`);
}
return <WrappedComponent {...this.props} />;
}
};
}
// Usage
OnlyValidProps(class MyClass extends React.Component {
static propTypes = {
x: React.PropTypes.number,
y: React.PropTypes.number
}
});
source
share