How to change mui button color on click in react js?
December 05, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
To change mui button color on click in React js, set <Button onClick={handleClick} variant="contained" color={flag ? "primary" : "secondary"}>
. It will change mui button color on click.
Today, I am going to show you, how to change mui button color on click in react js.
Installation
Install the following packages to use mui button color on click in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
MUI material change mui button color on click example.
The below code is an example of changing mui button color on click using mui.
App.js
import React from "react";
import Button from '@mui/material/Button';
export default function ButtonClick() {
const [flag, setFlag] = React.useState(true);
const handleClick = () => {
setFlag(!flag);
};
return (
<Button
onClick={handleClick}
variant="contained"
color={flag ? "primary" : "secondary"}
>
button
</Button>
);
}
In the above code example, I have used the @mui/material
component and changed mui button color on click of MUI.
Check the output of the above code example.
All the best 👍