Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.