Skip to content

Instantly share code, notes, and snippets.

@velopert
Created June 12, 2016 11:37
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 velopert/c6bbf94ee503ced63f8c29c985430ad2 to your computer and use it in GitHub Desktop.
Save velopert/c6bbf94ee503ced63f8c29c985430ad2 to your computer and use it in GitHub Desktop.
class ValidationExample extends React.Component {
/* ... */
}
Content.propTypes = {
// JS primitive types
optionalArray: React.PropTypes.array,
optionalBool: React.PropTypes.bool,
optionalFunc: React.PropTypes.func,
optionalNumber: React.PropTypes.number,
optionalObject: React.PropTypes.object,
optionalString: React.PropTypes.string,
// anything that can be rendered ( numbers, string, elements, array, fragment)
optionalNode: React.PropTypes.node,
// React element
optionalElement: React.PropTyps.element,
// instance of specific class
optionalMessage: React.PropTypes.instanceOf(Message),
// limited to specific values
optionalEnum: React.PropTypes.oneOf(['News', 'Photos']),
// one of many types
optionalUnion: React.PropTypes.oneOfType([
React.PropTypes.string,
React.propTypes.number
]),
// array of specific type
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
// object with property values of a certain type
optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
// object with particular shape
optionalObjectWithShape: React.PropTypes.shape({
color: React.PropTypes.string,
fontSize: React.PropTypes.number
}),
// Required function
requiredFunc: React.PropTypes.func.isRequired,
// Required prop with any data type
requiredAny: React.PropTypes.any.isRequired,
// custom validator
customProp: function(props, propName, componentName) {
if (!/matchme/.test(props[propName])) {
return new Error('Validation failed!');
}
}
};
/* ... */
export default ValidationExample;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment