Open In App

How to use x-grid-data-generator Module in ReactJS ?

Last Updated : 10 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Whenever we want to generate some data and build demos for the grid Component, we can use the x-grid-data-generator module in React JS. It has a large variety of data sets to play around with. DataGrid Component helps in displaying the information in a grid-like format of rows and columns. We can use the following approach in ReactJS to use the x-grid-data-generator module.

Prerequisites:

Approach:

To use x-grid-data-generator Module in ReactJS we will import the useDemoData function from this module and then use a common data set from this function by passing the DataSet name along with rowLength and maxColumns attribute. And after that, we have passed the data object to our DataGrid Component to display it to the user.

Steps to create React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: After creating the ReactJS application, Install the material-ui module using the following command:

npm i @material-ui/data-grid @material-ui/x-grid-data-generator

Project Structure:

Project Structure

The updated dependencies in package.json file will look like.

"dependencies": {
"@material-ui/data-grid": "^4.0.0-alpha.37",
"@material-ui/x-grid-data-generator": "^4.0.0-alpha.37",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example: This example uses useDemoData and DataGrid components from MUI for dummy data to implement x-grid-data-generator Module in ReactJS

App.js




import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { useDemoData } from '@material-ui/x-grid-data-generator';
 
export default function App() {
 
  const { data } = useDemoData({
    dataSet: 'Commodity',
    rowLength: 1000,
    maxColumns: 6,
  });
 
  return (
   <div style={{ height: 500, width: '80%' }}>
    <h4>
     How to use fetch Random Data Set in
     DataGrid Component in ReactJS?
    </h4>
     <DataGrid  {...data}/>
   </div>
  );
}


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads