Creating on/off button in react

158
February 19, 2022, at 05:40 AM

I am making a button that is going to say "disable" when its on and "enable" when its off. how do I do that in react?? I have tried to make it but I have no idea where to even start, is there some syntax im missing?

Answer 1

The code below does the job of displaying the button text based on a state. Inside the useState() hook you can specify the initial state. On the button's click event the state will be reversed to toggle between true and false

const customButton = () => {
 const [enabled, setEnabled] = useState(false);
 return (
  <button 
    onClick={() => setEnabled(!enabled)}>
    {enabled ? "disable" : "enable"}
  </button>
 );
}
Rent Charter Buses Company
READ ALSO
Color changeable SVG?

Color changeable SVG?

I wish to build a website where icon colors can be easily changed with the only change of the cssI've been given a set of custom icons in form of svg files

131
Printing using JS and @media print CSS but content pushed down

Printing using JS and @media print CSS but content pushed down

My content in the print preview window is displaying down about 5 or 6 lines

154
I&#39;m having issues with my style in laravel

I'm having issues with my style in laravel

I'm getting crazy with this matter and seems i can't find a solutionI started a laravel project and i'm unable to get the css working correctly

138