Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

changing the variant of the Button onClick MUI

I want to change the variant of the Button component whenever it gets clicked, from ‘outlined’ to ‘contained’:

<Button variant='outlined'>Click me to change my variant to contained</Button>

Is that possible in MUI? using React refs? or how can I achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can achieve it like this:

import React, { useState } from 'react';


    function App() {
      const [currentButtonVariant, setCurrentButtonVariant] = useState('outlined');
    
      const handleButtonVariantChange = () => {
        if (currentButtonVariant === 'outlined') {
          setCurrentButtonVariant('contained');
        }
        else {
          setCurrentButtonVariant('outlined');
        }
      }
    
      return (
        <div className="App">
            <Button variant={currentButtonVariant} onClick={handleButtonVariantChange}>Click me to change my variant to contained</Button>
        </div>
      );
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading