简体   繁体   English

如何使React Material-UI中的GridList组件响应

[英]How to make the GridList component in React Material-UI responsive

I'm using the Material-UI version 1 to make the grid UI in my React application. 我正在使用Material-UI版本1在我的React应用程序中创建网格UI。 I want to make the grid responsive. 我想让网格响应。 The GridList component API has cols props, which by default is 2 . GridList组件API有cols props,默认为2

For example, it could looks like this below. 例如,它可能如下所示。

<GridList cols={1} spacing={32} className="list"></GridList>

Can I dynamically change the props? 我可以动态更改道具吗? Or are there any other ways to make it responsive? 或者还有其他方法可以使其响应吗?

U can use the layout breakpoints provide by MUI to toggle GridList or GridListTile cols property. U可以使用MUI提供的布局断点来切换GridList或GridListTile cols属性。 The code will look like this: 代码如下所示:

...
import withWidth, { isWidthUp } from '@material-ui/core/withWidth';
...

const MyComponent = props => {
  const getGridListCols = () => {
    if (isWidthUp('xl', props.width)) {
      return 4;
    }

    if (isWidthUp('lg', props.width)) {
      return 3;
    }

    if (isWidthUp('md', props.width)) {
      return 2;
    }

    return 1;
  }

  return(
    ...
    <GridList spacing={15} cellHeight={400} cols={getGridListCols()}>
      {
        myItemsArray.map(item => (
          <GridListTile key={item} cols={1}>
            <FooBar />
          </GridListTile>
        ))
      }
    </GridList>
    ...
  );
};

...

export default withWidth()(MyComponent);

You can choose the cols value depending on the width ( xs, sm, md, lg, xl ), look at this example where 1 column is set for smaller screens. 您可以根据宽度( xs, sm, md, lg, xl )选择cols值,请查看此示例,其中为较小的屏幕设置了1列。

import React from "react";
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';

// @material-ui/core components
import withStyles from "@material-ui/core/styles/withStyles";
import withWidth from '@material-ui/core/withWidth';
import GridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';

const styles = theme => ({ });

class MyComponent extends React.Component {
    render() {
        const { classes, currentUser, images, width } = this.props;

        let columns = width === 'xs' || width === 'sm'  ? 1 : 2;

        return (
            <div className={classes.root}>
                <GridList cellHeight={180} cols={columns} className={classes.gridList}>
                    {images.map(image => (
                    <GridListTile key={image.id}>
                        <img src={ image.path } />
                        <GridListTileBar
                            title={ image.name }
                        />
                    </GridListTile>
                    ))}
                </GridList>
            </div>
        );
    }
}

MyComponent.propTypes = {
    currentUser: PropTypes.object,
    images: PropTypes.array.isRequired
};

function mapStateToProps(state) {
    return {
        currentUser: state.user.currentUser
    };
}

export default compose(
  withStyles(styles, {
    name: 'MyComponent',
  }),
  withWidth(),
  connect(mapStateToProps),
)(MyComponent);

When you say you want to make the grid responsive, do you perhaps mean adding more items dynamically? 当你说你想让网格响应时,你是否意味着动态添加更多项目? If not, by default Material-UI is responsive (meaning your viewport is dynamically sized depending on the device you view on), and depending on your GridList properties (like cols, rows, spacing etc) you can determine the style. 如果没有,默认情况下, Material-UI是响应式的(意味着您的视口的动态大小取决于您查看的设备),并且根据您的GridList属性(如cols,rows,spacing等),您可以确定样式。 If you have a look at https://material-ui-next.com/demos/grid-list/ there are a few examples to get you started. 如果您查看https://material-ui-next.com/demos/grid-list/ ,可以通过几个示例来帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM