I have a Card component and a CardGroup component, and I\'d like to throw an error when CardGroup has children that aren\'t Card
For those like me, using the TypeScript version. You can filter/modify components like this:
this.modifiedChildren = React.Children.map(children, child => {
if (React.isValidElement(child) && (child as React.ReactElement).type === Card) {
let modifiedChild = child as React.ReactElement;
// Modifying here
return modifiedChild;
}
// Returning other components / string.
// Delete next line in case you dont need them.
return child;
});