Material UI Grid width shrinks on every re-render - javascript

I am trying to render a list of 25 words on a 5x5 grid using the MUI Grid component. The 5x5 grid itself is a <Grid container direction="column"> containing five <Grid item>. Within each of those five <Grid item> is the following structure:
<Grid container direction = "row">
<Grid item xs={2} key={1}>
<Paper>
<Typography> word </Typography>
</Paper>
<Grid>
<Grid item xs={2} key={2}>
...
</Grid>
...
<Grid item xs={2} key={5}>
...
</Grid>
</Grid>
It renders as expected initially. However, whenever the component re-renders, the width of the grid decreases bit-by-bit on each re-render.
I've recreated the issue here:

You <Grid item> has no size defined. Set size accordingly :
Example
<Grid container direction = "row">
<Grid item xs={12} sm={6} md={3} lg={3} xl={2}>
<Paper>
<Typography> word </Typography>
</Paper>
<Grid>
</Grid>
Also there is no prop for Grid like key. Its a attribute for React to identify component in a map function. It has nothing to do with material UI
Updated
From
<Grid item md={12}>
<Grid container alignItems="center" justify="center">
{wordRows}
</Grid>
</Grid>
To
<Grid item container alignItems="center" justify="center">
{wordRows}
</Grid>

Related

How do I make this into 4 column in one row and also responsive as well?

I have this Grid and Card where I wanted to make it into 4 columns in one row. Currently, the 4th column goes in the 2nd row, like this:
I wanted to put the 4 columns in one row.
Also, if it's on a small screen, why does it look like this? The columns are not arranged.
codesandbox link: https://codesandbox.io/s/4-boxes-in-one-row-0mym3j?file=/demo.js
Codes:
<Container style={{ marginTop: "1rem", marginBottom: "1rem" }}>
<Box sx={{ "& h1": { m: 0 } }}>
<Grid container spacing={2} justify="flex-start">
<Grid item xs={12} sm={6} md={4}>
<Card>
<CardContent>
<Stack direction="row" spacing={2}>
<Typography variant={"h6"} gutterBottom>
Column 1
</Typography>
</Stack>
</CardContent>
</Card>
</Grid>
<Grid item xs={12} sm={6} md={4} order={{ xs: 3, sm: 2 }}>
<Card>
<CardContent>
<Stack direction="row" spacing={2}>
<Typography variant={"h6"} gutterBottom>
Column 2
</Typography>
</Stack>
</CardContent>
</Card>
</Grid>
<Grid item xs={12} sm={6} md={4} order={{ xs: 2, sm: 3 }}>
<Card>
<CardContent>
<Stack direction="row" spacing={2}>
<Typography variant={"h6"} gutterBottom>
Column 3
</Typography>
</Stack>
</CardContent>
</Card>
</Grid>
<Grid item xs={12} sm={6} md={4} order={{ xs: 2, sm: 3 }}>
<Card>
<CardContent>
<Stack direction="row" spacing={2}>
<Typography variant={"h6"} gutterBottom>
Column 4
</Typography>
</Stack>
</CardContent>
</Card>
</Grid>
</Grid>
</Box>
</Container>
So what you have to do to your child Grid is, instead of md={4}, give it md={3} (so for medium and above sized screens all 4 will be in single row). Or if you want md to remain 4 then add another breakpoint of lg={3} for all.
The row has 12 columns so either 3 items of 4 columns each (3*4 =12) or 4 items of 3 columns each. Keep the 12 rule in mind. And on smaller screens it will wrap automatically.
Now sm={6} mean that on small sized screens only 2 items will be in single row and next would wrap. So responsive is working fine. If you want 4 on small screens as well then you have to do sm={3} as well.
So you this line will change:
<Grid item xs={12} sm={6} md={3}> <======== md=3 OR
<Grid item xs={12} sm={6} md={4} lg={3}> <======= lg=3 added and md remain same

Adjusting the Div heights in Material UI using grid

For my project I'm tasked to use material-ui. Is there a way to reduce the width height of the div containing "Sign In With" text as shown in pic to bring the buttons closer to the text?
From this:
To this:
The code:
<div className={classes.root}>
<Grid
container
direction="row"
spacing={0}
>
<React.Fragment>
<Grid item xs={6} container direction="row">
<Grid item xs={12}>
<h1>Sign In With</h1>
</Grid>
<Grid item xs={12} container>
<Grid item xs={3}>
<Button
className={classes.buttonGoogle}
onClick={() => {
if (props.onSelectGoogle !== undefined)
props.onSelectGoogle('google');
}}
>
Google
</Button>
</Grid>
<Grid item xs={3}>
<Button
className={classes.buttonLinkedIn}
onClick={() => {
if (props.onSelectLinkedIn !== undefined)
props.onSelectLinkedIn('linkedin');
}}
>
LinkedIn
</Button>
</Grid>
</Grid>
</Grid>
<Grid item xs={6}>
<div className={classes.LoginImage}></div>
</Grid>
</React.Fragment>
</Grid>
</div>
The First layer Grid is enough, the inside one just complicates stuff.
I removed the inside grid (the one containing title and buttons).
You can add some styles to title and buttonsContainer
<div className={classes.root}>
<Grid container direction="row" spacing={0} >
<Grid item xs={6} >
<h1 className={classes.title} >Sign In With</h1>
<div className={classes.buttonsContainer} >
<Button
className={classes.buttonGoogle}
onClick={() => {
if (props.onSelectGoogle !== undefined)
props.onSelectGoogle('google');
}}
>Google</Button>
<Button
className={classes.buttonLinkedIn}
onClick={() => {
if (props.onSelectLinkedIn !== undefined)
props.onSelectLinkedIn('linkedin');
}}
>LinkedIn</Button>
</div>
</Grid>
<Grid item xs={6}>
<div className={classes.LoginImage}></div>
</Grid>
</Grid>
</div>

How to create rows using material-ui grid

I am using material-ui grid for responsive stuff. But i am not able to create nested rows for my view. I need first 1 row after that i am dividing into 3 columns with 3,3,6.For first column i need to divide into two rows which should match the height of 2 and 3 columns.I don't know how to separate column into two rows.
code:
<Grid container spacing={1}>
<Grid container item xs={12} spacing={3}>
<Grid item xs={3}>
<Grid container item xs={12} spacing={3}>
<Grid item xs={12}>
<Grid container item xs={12} spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</Grid>
<Grid container item xs={12} spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</Grid>
</Grid>
</Grid>
</Grid>
<Grid item xs={3}>
<Grid container item xs={12} spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</Grid>
</Grid>
<Grid item xs={6}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</Grid>
</Grid>
You should put item xs.={...} only on the items but not on the contsiners.
Nexts, you propably want to define sm={...} (and the other screen sizes) on the items as well. A xs with 12 will always take the full row.
Have a look on the documentation for examples https://material-ui.com/components/grid/
I think this should be enough for your case:
<Grid container>
<Grid container item xs={3}>
<Grid item xs={12} style={{border: "black solid 1px"}}>
column 1, row 1
</Grid>
<Grid item xs={12} style={{border: "black solid 1px"}}>
column 1, row 1
</Grid>
</Grid>
<Grid item xs={3} style={{border: "black solid 1px"}}>
column 2
</Grid>
<Grid item xs={6} style={{border: "black solid 1px"}}>
column 3
</Grid>
</Grid>

React how to expand spacing

I'm using Grid from #material-ui to display my data . Inside this Grid, I have multiple other grids, each represent a different section . I've use container spacing attribute of Grid, and gave it the max value possible (40) , but I need the spacing to be a litter bigger, and I can't figure how to do it..
<Paper className={this.props.classes.root}>
<Grid container spacing={40} >
<Grid item sm={6}>
..
</Grid>
<Grid item sm={2}>
..
</Grid>
<Grid item sm={4}>
..
</Grid>
</Grid>
</Paper>
How can I expand the spacing ?
Normally spacing props add padding between the elements in the grid. so you can use the className or inline style to achieve padding between elements.
Sample code give below for inline style.
<Grid container >
<Grid item sm={6} style={{padding:'30px'}}>
..
</Grid>
<Grid item sm={2} style={{padding:'30px'}}>
..
</Grid>
<Grid item sm={4} style={{padding:'30px'}}>
..
</Grid>
</Grid>

How to organize material-ui Grid into rows?

I'm using material-ui to do a form, using the Grid system I'd like to do the following:
<Grid container>
<Grid item xs={4} />
<Grid item xs={4} />
<Grid item xs={4} />
</Grid>
And be able to put the first 2 items, on the first row and the third on a second row, the only way I found to do it is:
<Grid container>
<Grid item xs={4} />
<Grid item xs={4} />
</Grid>
<Grid container>
<Grid item xs={4} />
</Grid>
What is the better way to stack material-ui Grid into rows (like the row class concept in Bootstrap grid)?
I also thought about these options:
filling the first row with empty Grid item?
putting vertical container?
You are close with the second block of code.
I found that you could simply create 2 distinct Grid sections such as:
<div>
<Grid id="top-row" container spacing={24}>
<Grid item xs={4}>
<Paper className={classes.paper}>Grid cell 1, 1</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>Grid cell 2, 1</Paper>
</Grid>
</Grid>
<Grid id="bottom-row" container spacing={24}>
<Grid item xs={4}>
<Paper className={classes.paper}>Grid cell 1, 2</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>Grid cell 2, 2</Paper>
</Grid>
</Grid>
</div>
You can play with it in my sandbox
It might also be work checking out the official documentation for Grid, as it shows a few ways to use it and also links to each exapmle hosted on codesandbox.io so you can play with it yourself.
From the docs, it seems the best way to have multi-layered grid systems is to define the width of the overall grid and then to define the width of each cell, as this will push cells later in the series onto the other rows.
Here's another approach you could take which does not require two grid containers. In Material UI you can organise a grid into rows by using the fact that a row has 12 columns and making use of empty grid items (as Material UI does not support offset grid items) e.g.
<Grid container spacing={24}>
<Grid item xs={4}>
<Paper className={classes.paper}>Grid cell 1, 1</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>Grid cell 2, 1</Paper>
</Grid>
<Grid item xs={4} />
{/* 12 Columns used, so next grid items will be the next row */}
{/* This works because Material UI uses Flex Box and flex-wrap by default */}
<Grid item xs={4}>
<Paper className={classes.paper}>Grid cell 1, 2</Paper>
</Grid>
<Grid item xs={8} />
</Grid>
You can see this working here.
Indeed this is actually demonstrated on the Material UI websites (though without the offsets) here.
I admit though that I'd prefer to see semantic Row and Column elements in Material UI (e.g. like in Bootstrap), but this is not how it works.
You can do something like this
<Grid container spacing={1}>
<Grid container item xs={12} spacing={3}>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</Grid>
<Grid container item xs={12} spacing={3}>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</Grid>
<Grid container item xs={12} spacing={3}>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</Grid>
</Grid>
I created new component to solve this problem:
export const GridBreak = styled.div`
width: 100%
`;
Then your code would look like this:
<Grid container>
<Grid item xs={4} />
<Grid item xs={4} />
<GridBreak />
<Grid item xs={4} />
</Grid>

Categories

Resources