【问题标题】:How to make each Card the same size in my Material Grid如何在我的材质网格中使每张卡片的大小相同
【发布时间】:2023-03-17 21:43:01
【问题描述】:

我学习 React 并遵循这个 tutorial 但即使我有相同的代码,结果也不一样。从教程中我想要一个像这样的网格:

Image from the Tutorial

但我得到的是:

Image from my code

import React, { Component } from 'react';
import { connect } from "react-redux";
import { withStyles } from '@material-ui/styles';
import { Grid, Paper, Typography } from "@material-ui/core";
import Card from "@material-ui/core/Card";
import CardActionArea from "@material-ui/core/CardActionArea";
import CardContent from "@material-ui/core/CardContent";
const images = require.context('../../public/images', true);

export class Posts extends Component {
    constructor() {
        super();
    }

    componentDidUpdate(prevProps) {

    }
    render() {
        const { classes } = this.props;
        return (
            <div style={{ marginTop: 20, padding: 30 }}>
                <Grid container spacing={40} justify="center">
                    {this.props.books.map(post => (
                        <Grid item key={post.title} Box width={1 / 4}>
                            <Card>
                                <CardActionArea>

                                    <CardContent>
                                        <Typography gutterBottom variant="h5" component="h2">
                                            {post.title}
                                        </Typography>
                                        <Typography component="p">{post.description}</Typography>
                                        <Typography component="p">{post.author}</Typography>
                                        <Typography component="p">{post.genre}</Typography>
                                        <Typography component="p">{post.publish_date}</Typography>
                                        <Typography component="p">{post.price}</Typography>

                                    </CardContent>


                                </CardActionArea>
                            </Card>
                        </Grid>
                    ))}
                </Grid>
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        books: state.reducer.booksList
    }
}

export default connect(mapStateToProps)(withStyles(styles)(Posts));

我做错了什么?我认为这是迫使卡片变大的文本,但我还没有阅读过如何限制文本(如果可能的话)

【问题讨论】:

    标签: reactjs react-material


    【解决方案1】:

    您可以将样式应用于卡片

     var cardStyle = {
        display: 'block',
        width: '30vw',
        transitionDuration: '0.3s',
        height: '45vw'
    }
    

    在您的 CardStyle 中,您可以应用上述样式

      <Card style={cardStyle}>
                    <CardHeader
                      title="URL Avatar"
                      subtitle="Subtitle"
                      avatar="https://placeimg.com/800/450/nature"
                    />
    

    【讨论】:

      【解决方案2】:

      您的post.titles 很长。您可以使用fluid grid。从Grid item 中删除Box width={1 / 4}

      <Grid item key={post.title} xs={3}>
      

      xs={3} 缩放到容器的 1/4。

      您还可以设置 noWrap 属性以显示长标题的省略号而不是换行。

      <Typography noWrap ...>
        {post.title}
      </Typography>
      

      【讨论】:

        猜你喜欢
        • 2019-09-13
        • 1970-01-01
        • 1970-01-01
        • 2021-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多