31
loading...
This website collects cookies to deliver better user experience
Children
to wrap the child components with a box component.function MyComponent(): JSX.Element {
return (
<CardContent>
<Box mr={1] mb={1}>
<Chip label="Expired" />
</Box>
<Box mr={1] mb={1}>
<Chip label="Pending" />
</Box>
<Box mr={1] mb={1}>
<Chip label="Valid" />
</Box>
</CardContent>
);
}
<Chip />
components. The Material UI <Box />
component is just noise and it also adds extra nesting. And we don't like to much nesting, it's hard to read and even harder to review in a PR.<CardContent />
add that <Box />
component around it childrens automatically with a thing called React.Children
. Children
which allows you to iterate all the children.import { Children, isValidElement } from 'react';
// other imports
export default function CardWithPaddedContent(props: Props): JSX.Element {
const classes = useStyles(props);
const { children, ...rest } = props;
const childrenWithPadding = Children.map(children, (child) => {
if (!isValidElement(child)) {
return null;
}
return <Box mb={1} mr={1}>{child}</Box>;
});
return (
<CardContent {...rest} classes={classes} >
{childrenWithPadding }
</CardContent >
);
}
<Box />
component with some bottom and right marginfunction MyComponent(): JSX.Element {
return (
<CardWithPaddedContent>
<Chip label="Expired" />
<Chip label="Pending" />
<Chip label="Valid" />
</CardWithPaddedContent>
);
}
<Chip />
component you might wonder