Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI Grid does not hide when using display

I want to use MUI Grid and I wanted to hide one item Grid if the screen is small, so I found something called Display. My code looks like this:

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>
  );
}

I don´t uderstand why it doesn't work (the text YY still appears) . Can't I use display with Grid maybe? If yes then why?

like image 298
user0810 Avatar asked Oct 01 '19 17:10

user0810


People also ask

How do you hide the grid in material UI?

By default, all the columns are visible. The column's visibility can be switched through the user interface in two ways: By opening the column menu and clicking the Hide menu item. By clicking the Columns menu and toggling the columns to show or hide.

How do I hide components in material UI?

Material UI Hidden component is used to change the visibility of any other component. We can use this component to hide a component even on different breakpoints. For example, if you have one sidebar that you want to hide on small screen devices, you can use this component with breakpoints.

What is SM in material UI grid?

xs, extra-small: 0px. sm, small: 600px. md, medium: 900px. lg, large: 1200px.

What is Xs in grid MUI?

For example, xs={12} sizes a component to occupy the whole viewport width regardless of its size. The Auto-layout makes the items equitably share the available space. That also means you can set the width of one item and the others will automatically resize around it.


1 Answers

The style functions are not automatically supported by the Grid component.

The easiest way to leverage the style functions is to use the Box component. The Box component makes all of the style functions (such as display) available. The Box component has a component prop (which defaults to div) to support using Box to add style functions to another component.

The Grid component similarly has a component prop, so you can either have a Grid that delegates its rendering to a Box or a Box that delegates to a Grid.

The example below (based on your code) shows both ways of using Box and Grid together.

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);

Edit Use system style functions with Grid

like image 155
Ryan Cogswell Avatar answered Sep 21 '22 08:09

Ryan Cogswell