Skip to content

Instantly share code, notes, and snippets.

@nnarhinen
Last active March 15, 2016 09:46
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 nnarhinen/c062d2ffa68c50948292 to your computer and use it in GitHub Desktop.
Save nnarhinen/c062d2ffa68c50948292 to your computer and use it in GitHub Desktop.
OR validation for React props
const Component = React.createClass({
propTypes: {
file: requireThisOr('files', React.PropTypes.instanceOf(File).isRequired),
files: requireThisOr('file', React.PropTypes.arrayOf(React.PropTypes.instanceOf(File)).isRequired)
}
})
export default function requireThisOr(otherProp, propValidator) {
return (props, propName, componentName) => {
if (!props[otherProp]) {
return propValidator(props, propName, componentName);
}
};
}
@nnarhinen
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment