I'm using react with material UI, I have 40 dynamic cards in an array and when I render them , I want to have 3 cards in a row and I get all the cards in one column. I'm using this card: https://codesandbox.io/s/r084q99q34
Maybe you can use Flexbox ? I had the same problem and I resolved it thanks to FlexBox trough material ui. Also, be sure to use a material core version superior or equal to 4. Hope it helps !
import React from 'react'
import { makeStyles } from '@material-ui/core/styles'
import {
Grid,
Card,
CardContent,
Typography,
CardHeader
} from '@material-ui/core/'
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
padding: theme.spacing(2)
}
}))
export default function AltCard() {
const classes = useStyles()
const data = [
{ quarter: 1, earnings: 13000 },
{ quarter: 2, earnings: 16500 },
{ quarter: 3, earnings: 14250 },
{ quarter: 4, earnings: 19000 }
]
return (
<div className={classes.root}>
<Grid
container
spacing={2}
direction="row"
justify="flex-start"
alignItems="flex-start"
>
{data.map(elem => (
<Grid item xs={12} sm={6} md={3} key={data.indexOf(elem)}>
<Card>
<CardHeader
title={`quarter : ${elem.quarter}`}
subheader={`earnings : ${elem.earnings}`}
/>
<CardContent>
<Typography variant="h5" gutterBottom>
Hello World
</Typography>
</CardContent>
</Card>
</Grid>
))}
</Grid>
</div>
)
}
You could use the Grid component for this, e.g.
<Grid container spacing={24}>
<Grid item md={3}>
<Demo />
</Grid>
<Grid item md={3}>
<Demo />
</Grid>
<Grid item md={3}>
<Demo />
</Grid>
</Grid>
https://codesandbox.io/s/rzmz5jnom
Material UI provides GridList
and GridListTile
components. That let you configure it pretty well.
Grid lets you define columns and cells. A tile
takes up one slot and it keeps fitting them in columns and overflowing them automatically if required. You can read more in the official documents and take a look at their samples for inspiration.
let nums = Array.from(Array(40).keys());
render(
<GridList cols={3}>
{nums.map(n => {
return (
<GridListTile key={n}>
<Demo key={n} num={n} />
</GridListTile>
);
})}
</GridList>,
rootElement
);
See a forked sandbox: https://codesandbox.io/s/5yjpp339w4
If for some reason the inbuilt components do not suit your needs or if grid still confuses you, do take a look at their interactive layout examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With