Created
December 6, 2018 10:12
-
-
Save Fer0x/5615c199c353ca59422b91836fe54fe4 to your computer and use it in GitHub Desktop.
PropTypes.component
This file contains hidden or 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
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