Open In App

React MUI Checkbox API

Material-UI is a user interface framework that provides pre-defined and customizable React components for faster and easy web development. The Material-UI components are based on top Material Design by Google. In this article let’s discuss the Checkbox API in the Material-UI library.

Checkbox API offered by MUI: Checkbox API is used to allow users to select multiple items from a set of items. 



Checkbox props:

 



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 required module using the following command:

npm install @mui/material

Project Structure:  It will look like the following.

 

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

npm start

Example 1: In this example, we will try to create a simple application that displays a list of checkbox components in different states.

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

Filename: App.js




import * as React from 'react';
import Checkbox from '@mui/material/Checkbox';
  
export default function BasicButtonGroup() {
    return (
        <div>
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Checkbox API</strong>
                <br />
                <br />
            </div>
            <div
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <Checkbox defaultChecked />
                <Checkbox />
                <Checkbox disabled />
                <Checkbox disabled checked />
            </div>
        </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:

 

Example 2: In this example, let’s create a simple application that displays checkbox components with their respective labels. Change your App.js like the one below.




import * as React from 'react';
import Checkbox from '@mui/material/Checkbox';
  
export default function BasicButtonGroup() {
    return (
        <div>
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Checkbox API</strong>
                <br />
                <br />
            </div>
            <div
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <Checkbox color='secondary' />
                <Checkbox color='success' />
                <Checkbox color='default' />
                <Checkbox color='primary' />
            </div>
        </div>
    );
}

Output:

 

Reference: https://mui.com/material-ui/api/checkbox/


Article Tags :