The link you provided to the documentation example includes an extra div
surrounding the code snippet you shared:
<div style={{ height: 400, width: '100%' }}>
. Adding this wrapper element eliminates the console error by giving the element a specified height mentioned in the warning message. You can view a modified version of your stackblitz without the error here:
https://stackblitz.com/edit/react-m2uoq1?file=src%2FApp.js.
You have the flexibility to replace the 400px height with any desired value—just avoid using percentages. To make the DataGrid
occupy the full height of the browser, consider using 100vh
instead of 100%
.
Here is a code snippet as an example:
import * as React from "react";
import { DataGrid } from "@mui/x-data-grid";
import { useDemoData } from "@mui/x-data-grid-generator";
import CssBaseline from "@mui/material/CssBaseline";
export default function FlexLayoutGrid() {
const { data } = useDemoData({
dataSet: "Commodity",
rowLength: 50,
maxColumns: 6
});
return (
<>
<CssBaseline />
<div style={{ height: "100vh", width: "100%" }}>
<div style={{ display: "flex", height: "100%" }}>
<div style={{ flexGrow: 1 }}>
<DataGrid {...data} />
</div>
</div>
</div>
</>
);
}
https://codesandbox.io/s/full-height-datagrid-72hi1v?fontsize=14&hidenavigation=1&theme=dark
In my example, I integrated the CssBaseline
component to remove the default 8px margin on the <body>
element—this prevents the appearance of a scroll bar when using 100vh
; however, you can adjust for the default margin (or a custom margin in your application) using methods like calc
(e.g.,
height: "calc(100vh - 16px)"
).