![]() |
VOOZH | about |
Whenever the user wants to group a related option, the ToggleButtonGroup component is used. Material UI for ReactJS has this component available for us, and it is very easy to integrate. We can use the following approach in ReactJS to use ToggleButtonGroup component.
To use ToggleButtonGroup Component in React JS we will install the material UI packages. Create multiple buttons using button components with toggleButtonGroup to show the toggle animation when clicked.
Step 1: Create a React application using the following command:
npx create-react-app foldernameStep 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldernameStep 3: After creating the ReactJS application, Install the material-ui module using the following command:
npm i @mui/material @mui/icons-materialProject Structure:
The updated dependencies in package.json file will look like.
"dependencies": {
"@mui/icons-material": "^5.14.16",
"@mui/material": "^5.14.17",
"@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 1: This example uses ToogleButtonGroup Component from MUI to implement toggle animation for mutliple buttons.
Step to Run Application: Run the application using the following command from the root directory of the project:
npm startOutput: Now open your browser and go to http://localhost:3000/, you will see the following output:
Example 2: This example uses ToggleButtonGroup component to implement multi selection input.
Step to Run Application: Run the application using the following command from the root directory of the project:
npm startOutput: Now open your browser and go to http://localhost:3000/, you will see the following output: