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

sfl*_*low 8 reactjs material-ui responsive

我正在使用Material-UI版本1在我的React应用程序中制作网格UI。我想使网格响应。GridList组件API具有cols属性,默认情况下为2

例如,如下所示。

<GridList cols={1} spacing={32} className="list"></GridList>
Run Code Online (Sandbox Code Playgroud)

我可以动态更改道具吗?还是有其他方法使其具有响应性?

Mor*_*kop 16

U可以使用MUI提供的布局断点来切换GridList或GridListTile cols属性。该代码将如下所示:

...
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);
Run Code Online (Sandbox Code Playgroud)


Fel*_*ira 6

您可以cols根据宽度(xs, sm, md, lg, xl)选择值,请看此示例,其中为较小的屏幕设置了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);
Run Code Online (Sandbox Code Playgroud)


小智 -3

当您说想要使网格响应时,您可能是指动态添加更多项目吗?如果没有,默认情况下Material-UI是响应式的(意味着您的视口根据您查看的设备动态调整大小),并且根据您的GridList属性(如列、行、间距等),您可以确定样式。如果您查看https://material-ui-next.com/demos/grid-list/,这里有一些示例可以帮助您入门。

  • 这没有帮助,因为它没有回答他的要求。他问如何动态更改“cols”属性:) (3认同)