Skip to content

Instantly share code, notes, and snippets.

@davidaurelio
Created March 4, 2015 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidaurelio/eecf9796f3108de3fce9 to your computer and use it in GitHub Desktop.
Save davidaurelio/eecf9796f3108de3fce9 to your computer and use it in GitHub Desktop.
React: custom validator for type of children
const {forEach} = React.Children;
class Child extends React.Component { /*…*/ }
class Container extends React.Component { /*…*/ }
Container.propTypes = {
children: function(props, propName, componentName) {
forEach(props[propName], (element) => {
const {type} = element;
if (type !== Child) {
throw Error(`Invalid child for "${componentName}": "${type.displayName || type.name || type}"`);
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment