Skip to content

Instantly share code, notes, and snippets.

@Fer0x
Created December 6, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fer0x/5615c199c353ca59422b91836fe54fe4 to your computer and use it in GitHub Desktop.
Save Fer0x/5615c199c353ca59422b91836fe54fe4 to your computer and use it in GitHub Desktop.
PropTypes.component
import { isValidElementType } from 'react-is';
function PropTypeError(message) {
this.message = message;
this.stack = '';
}
PropTypeError.prototype = Error.prototype;
const createPropTypesComponent = isRequired => (
props,
propName,
componentName,
location,
propFullName,
) => {
const prop = props[propName];
propFullName = propFullName || propName;
if (prop == null) {
if (isRequired) {
if (prop === null) {
return new PropTypeError(
`The ${location} \`${propFullName}\` is marked as required ` +
`in \`${componentName}\`, but its value is \`null\`.`,
);
}
return new PropTypeError(
`The ${location} \`${propFullName}\` is marked as required in ` +
`\`${componentName}\`, but its value is \`undefined\`.`,
);
}
return null;
}
if (!isValidElementType(prop)) {
return new PropTypeError(
`Invalid ${location} \`${propFullName}\` supplied to \`${componentName}\`, expected a valid React component.`,
);
}
return null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment