【发布时间】:2021-05-24 06:33:02
【问题描述】:
我在让网格系统正常工作时遇到问题。在一个文件中,我有这样的设置来呈现博客项目:
return (
<div>
{blogs != null
? (<Grid container direction="row" xs={12} md={4}>
{blogs.map((blog) => {
console.log(blog);
return (
<div key={blog._id}>
<CardComp data={blog} />
</div>
)
})}
</Grid>)
: ('loading....')}
</div>
);
在卡片组件文件中我有以下代码:
return (
<Grid item direction="row">
{data ? (
<Card className={classes.root} variant="outlined" >
<CardContent>
<Typography variant="h5" component="h2">
{blogs.title}
</Typography>
<Typography className={classes.pos} color="textSecondary">
{renderDate(blogs.createdAt)}
</Typography>
<Typography variant="body2" component="p">
<p>{blogs.description.substring(0,200)}</p>
</Typography>
</CardContent>
<Grid container alignItems={"center"} alignContent={"space-between"} className={classes.bottom} >
<Grid item>
<CardActions>
<Button size="small" variant={"outlined"}>Read More</Button>
</CardActions>
</Grid>
<Grid item>
<Grid container justify={"space-between"}>
{blogs.tags.slice(0,3).map((tag, index) => {
return(
<>
<Button variant={"outlined"} color={"primary"} size={"small"}>{tag}</Button>
</>
)
})}
</Grid>
</Grid>
</Grid>
</Card>
)
:('Loading....')}
</Grid>
);
【问题讨论】:
-
考虑在高度嵌套的代码块上使用 2 个空格的缩进。它使代码更具可读性,从而增加了快速回答的机会。在线美化器/格式化器可以自动为您完成。
-
尝试将网格和网格容器放在同一个组件中。您的网格容器应该在您映射之后出现,并且您的网格项目将包装您的卡片组件。有人提供的答案也很关键。
标签: reactjs material-ui