Created
March 4, 2015 15:07
-
-
Save davidaurelio/eecf9796f3108de3fce9 to your computer and use it in GitHub Desktop.
React: custom validator for type of children
This file contains 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
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