Hello am trying to build card sets using material UI I’ve sorted data from a javascript object but when I compile the result is not what am looking for here’s the code am using.
JavaScript
x
66
66
1
const CardFeatures = () => {
2
3
const [features, setFeatures] = useState([
4
{ title: 'Feature 1', body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do.', icon: <Assignment />, id: 1 },
5
{ title: 'Feature 2', body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do.', icon: <Autorenew />, id: 2 },
6
{ title: 'Feature 3', body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do.', icon: <Bookmark />, id: 3 },
7
{ title: 'Feature 4', body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do.', icon: <Bookmark />, id: 4 },
8
])
9
10
const useStyles = makeStyles((theme) => ({
11
root: {
12
minWidth: 275,
13
marginTop: theme.spacing(7),
14
marginBottom: theme.spacing(7),
15
borderRadius: 16,
16
elevation: 'FF0000',
17
boxShadow: 'rgb(140 152 164 / 25%) 0px 3px 6px 0px'
18
19
},
20
title: {
21
fontSize: 16,
22
marginLeft: theme.spacing(2),
23
fontWeight: "bold"
24
},
25
description: {
26
marginLeft: theme.spacing(2),
27
marginTop: theme.spacing(2),
28
fontSize: 22,
29
marginBottom: theme.spacing(2),
30
color: '#808080'
31
},
32
green: {
33
color: '#fff',
34
backgroundColor: green[500],
35
width: theme.spacing(6),
36
height: theme.spacing(6),
37
marginLeft: theme.spacing(2),
38
marginTop: theme.spacing(2),
39
marginBottom: theme.spacing(2)
40
}
41
}));
42
43
44
const classes = useStyles();
45
46
return (
47
<div>
48
{features.map(feature => (
49
50
<Card className={classes.root} key={feature.id} elevation='0'>
51
<CardContent>
52
<Avatar aria-label='recipe' className={classes.green}>{feature.icon} </Avatar>
53
<Typography className={classes.title} color='textPrimary' gutterBottom>
54
{feature.title}
55
</Typography>
56
<Typography className={classes.description}>
57
{feature.body}
58
</Typography>
59
</CardContent>
60
</Card>
61
))}
62
</div>
63
);
64
}
65
66
export default CardFeatures;
And the result is this:
instead of this:
P.S.: the second picture coded manually
Advertisement
Answer
i’ve tried this one and it worked for me
JavaScript
1
17
17
1
<Grid container spacing={3}>
2
{features.map(feature => (
3
<Grid item xs={12} md={6} lg={4}>
4
<Card className={classes.root} key={feature.id}>
5
<CardContent>
6
<Avatar className={classes.green}>{feature.icon} </Avatar>
7
<Typography className={classes.title} color='textPrimary' gutterBottom>
8
{feature.title}
9
</Typography>
10
<Typography className={classes.description}>
11
{feature.body}
12
</Typography>
13
</CardContent>
14
</Card>
15
</Grid>
16
))}
17
</Grid>
I wrapped the map with a container and wrapped the card with Grid and it worked