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

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 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 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
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
React-Questions
ReactJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to set background images in ReactJS ?
How to create a table in ReactJS ?
ReactJS useNavigate() Hook
How to navigate on path by button click in react router ?
React-Router Hooks
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript