【问题标题】:Grid Items are in a column instead of a row Material-UI网格项在列中而不是在行中 Material-UI
【发布时间】:2019-01-31 19:17:06
【问题描述】:

我正在尝试创建一个连续包含 3 个项目的网格。但是,我的代码中的网格垂直继续,每行只有一个项目。每个网格项目中也有一个图像。如何更改它,以便连续有 3 个网格项。这是我的代码:

Grid.js:

const styles = theme => ({

  root: {
    display: "flex",
    flexWrap: "wrap",
    justifyContent: "space-around",
    overflow: "hidden",
    backgroundColor: theme.palette.background.paper,
    margin: 0
  },


  paper: {
    margin: "1%",
    padding: "1%",
    width: "80%"
  },
      });

class GridListings extends Component {
  componentDidMount() {
    this.props.getPosts();
  }

  render() {
    const { classes } = this.props;
    const { posts, loading } = this.props.post;
    let postContent;

    if (posts === null || loading) {
      postContent = <div> loading</div>;
    } else {
      postContent = <PostFeed posts={posts} />;
    }

    return (
      <div className={classes.root}>
        <Paper className={classes.paper}>
          <Typography align="center" variant="display2">
            Sneaker Listings
          </Typography>

          {postContent}
        </Paper>
      </div>
    );
  }
}

postfeed.js:

class PostFeed extends Component {
  render() {
    const { posts } = this.props;

    return posts.map(post => <ListingPost key={post._id} post={post} />);
  }
}

ListingPost.js:

const styles = theme => ({

  root: {
    flexGrow: 1,
    maxWidth: "30%",
    padding: theme.spacing.unit * 2
  },
  image: {
    maxWidth: "100%",
    maxHeight: "100%"
  },
  img: {
    margin: "auto",
    display: "block",
    maxWidth: "100%",
    maxHeight: "100%"
  }
});

class ListingPost extends Component {
  onDeleteClick(id) {
    console.log(id);
  }

  render() {
    const { post, auth, classes } = this.props;

    return (
      <Paper className={classes.root}>
        <Grid container spacing={16} direction="row">
          <Grid item>
            <ButtonBase className={classes.image}>
              <img
                className={classes.img}
                alt="complex"
                src={post.productImage}
              />
            </ButtonBase>
          </Grid>
          <Grid item xs={12} sm container direction="row">
            <Grid item xs container spacing={16}>
              <Grid item xs>
                <Typography gutterBottom variant="subheading">
                  Shoes
                </Typography>
                <Typography gutterBottom>Size:12</Typography>
                <Typography color="textSecondary">Brand New</Typography>
              </Grid>
              <Grid item>
                <Typography style={{ cursor: "pointer" }}>Remove</Typography>
              </Grid>
            </Grid>
            <Grid item>
              <Typography variant="subheading">$19.00</Typography>
            </Grid>
          </Grid>
        </Grid>

      </Paper>
    );
  }
}

感谢任何帮助。

【问题讨论】:

  • 你能在这里分享工作代码和所需的输出,以便更容易解决问题。 ATM不清楚

标签: javascript css reactjs flexbox material-ui


【解决方案1】:

您必须在下面的组件中使用引导类row。试试下面的代码

PostFeed.js

class PostFeed extends Component {
  render() {
    const { posts } = this.props;
    let postss= [];
    posts.map(post => {
      postss.push(<ListingPost key={post._id} post={post} />);
    });
    return (
      <div className="row">
        {postss}
      </div>
    );
  }
}

在您的 grid.js 中添加带有 className 的 div row

<Paper className={classes.paper}>
  <Typography align="center" variant="display2">
    Sneaker Listings
  </Typography>
  <div className="row">
    {postContent}
  </div>
</Paper>

【讨论】:

  • 这行得通,非常感谢!但是,由于我使用的是 material-ui,所以使用 bootstrap 和 material-ui 有什么不好或错误的地方吗?
  • 我建议您使用 material-ui,因为您已经掌握了它。更轻量级,使用JSS编译css
猜你喜欢
  • 2020-06-24
  • 2019-10-03
  • 2021-05-24
  • 1970-01-01
  • 2019-01-31
  • 2021-08-16
  • 1970-01-01
  • 2017-09-14
  • 2018-11-17
相关资源
最近更新 更多