【问题标题】:Material-UI Grid does not hide when using displayMaterial-UI Grid在使用显示时不隐藏
【发布时间】:2020-01-31 01:11:42
【问题描述】:

我想使用 MUI Grid,如果屏幕很小,我想隐藏一项 Grid,所以我找到了一个名为 Display 的东西。我的代码如下所示:

function CRUDView() {
  return (
    <Grid
      container
      spacing={1}
      direction="row"
      justify="center"
      alignItems="center"
    >
      <Grid item xs={12} lg={6}>
        <span>XX</span>
      </Grid>
      <Grid item xs={6} display={{ xs: "none", lg: "block" }} >
        <span>YY</span>
      </Grid>
    </Grid>
  );
}

我不明白为什么它不起作用(文本 YY 仍然出现)。我不能将显示与Grid 一起使用吗?如果是,那为什么?

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    Grid 组件不会自动支持style functions

    利用样式函数的最简单方法是使用Box componentBox 组件使所有样式函数(例如display)都可用。 Box 组件有一个component prop(默认为div)来支持使用Box 向另一个组件添加样式函数。

    Grid 组件同样有一个component prop,因此您可以有一个Grid 将其渲染委托给Box 或一个Box 委托给Grid

    下面的示例(基于您的代码)显示了同时使用BoxGrid 的两种方式。

    import React from "react";
    import ReactDOM from "react-dom";
    
    import Grid from "@material-ui/core/Grid";
    import Box from "@material-ui/core/Box";
    import { makeStyles } from "@material-ui/core/styles";
    
    const useStyles = makeStyles({
      gridItem: {
        border: "1px solid red"
      }
    });
    
    function App() {
      const classes = useStyles();
      return (
        <Grid
          container
          spacing={1}
          direction="row"
          justify="center"
          alignItems="center"
        >
          <Grid className={classes.gridItem} item xs={12} lg={6}>
            <span>XX</span>
          </Grid>
          <Box
            component={Grid}
            className={classes.gridItem}
            item
            xs={3}
            display={{ xs: "none", lg: "block" }}
          >
            <span>YY</span>
          </Box>
          <Grid
            component={Box}
            className={classes.gridItem}
            item
            xs={3}
            display={{ xs: "none", lg: "block" }}
          >
            <span>ZZ</span>
          </Grid>
        </Grid>
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    【讨论】:

      【解决方案2】:

      Material UI 公开了一个&lt;Hidden&gt; 组件来实现这一点。只需为特定的屏幕尺寸包装您想要隐藏的组件:

            <Hidden xsDown >
                <p>Hide me on XS view port width.</p>
            </Hidden>
      

      您可以在documentation 中找到更多示例。

      【讨论】:

        【解决方案3】:

        MUI v5中,您可以直接在Grid 上更改display 的值,而无需再使用Box

        <Grid
          item
          xs={3}
          sx={{
            display: { xs: "none", lg: "block" }
          }}
        >
          <span>YY</span>
        </Grid>
        

        【讨论】:

          【解决方案4】:

          Grid Items 只是设置你的布局。

          它们实际上不显示任何内容。 MUI 显示选项用于隐藏特定元素。

          试试这个:

          function CRUDView() {
            return (
              <Grid
                container
                spacing={1}
                direction="row"
                justify="center"
                alignItems="center"
              >
                <Grid item xs={12} lg={6}>
                  <span>XX</span>
                </Grid>
                //removed from the below Grid Item
                <Grid item xs={12} lg={6}>
                  <span display={{ xs: "none", lg: "block" }}>YY</span>
                </Grid>
              </Grid>
            );
          }
          

          即使网格仍然存在,这也会隐藏单个 span 元素。

          【讨论】:

          • 当我删除 xs={6} 时,网格消失了,但里面的内容没有,所以我仍然可以看到 YY 文本
          • 对不起,我误解了你的问题,我编辑了我的答案以反映我认为你正在尝试做的事情。
          • 感谢您的回答,不幸的是它也不起作用。
          猜你喜欢
          • 1970-01-01
          • 2015-09-23
          • 2020-08-06
          • 2021-04-23
          • 1970-01-01
          • 2021-09-14
          • 1970-01-01
          • 2020-10-30
          • 2015-03-01
          相关资源
          最近更新 更多