Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 26 Apr, 2021

Whenever we want to generate some data and build demos for the grid Component, we can use the x-grid-data-generator module. It has a large variety of data set to play around. 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.

Approach: Follow these simple steps in order to use the x-grid-data-generator module in ReactJS. In the following example, we have imported the useDemoData function from this module and then used 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 for displaying it to the user.

Creating 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 install @material-ui/data-grid
    npm install @material-ui/x-grid-data-generator

Project Structure: It will look like the following.

Project Structure

Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

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:

Reference: https://www.npmjs.com/package/@material-ui/x-grid-data-generator

My Personal Notes arrow_drop_up
Recommended Articles
Page :