pressing the button again will yield a new outcome

I am looking to disable a button (material ui) when it is clicked for the second time by setting disabled={true}. Unfortunately, I have not been able to find any examples related to this specific scenario on StackOverflow.

<Button
  onClick={this.startDraw.bind(this)}
  disabled={}
  startIcon={<Brush />}
>

Initially, the button is clickable and triggers an action when clicked. However, my goal is to make sure that when the same button is clicked again for the second time, it becomes disabled and cannot be clicked anymore.

Answer №1

When using a Class component:

    state = {
    count: 0
    };
this.setState({ count: count + 1 }); // Ensure to use this line on your button click event

For the button section:

   <Button
    onClick={this.startDraw.bind(this)}
    disabled={state.count == 2}
    startIcon={<Brush />}
  />

If opting for a functional component, follow this approach:


   const [count , setCount ] = useState(0);
   setCount(count + 1) // Remember to utilize this line on your button triggering

    <Button
    onClick={this.startDraw.bind(this)}
    disabled={count == 2}
    startIcon={<Brush />}
/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing a date string to MM DD format in React without using plain JavaScript

I'm familiar with how to handle this outside of React. My issue is that I have a date string coming from an API within an object, and I need to reformat it. The current format is "2022-12-13T06:00Z" but I want it to display as "December 13". The objec ...

Forming triangles with outlines

Recently, I had the challenge of designing speech bubbles and found a clever technique to create the triangular tip at the end using CSS. By setting the element's width and height to 0 and playing around with borders, you can achieve diagonal shapes f ...

How can we toggle a function to expand or collapse fields generated in an ngFor loop?

One of my challenges involves managing a div that is repeated using *ngFor in Angular. This results in multiple divs on the page, each containing collapsible fields that can toggle. Essentially, I have nested collapsible fields within other collapsible fie ...

I need assistance on updating a document in MongoDB using Node.js

I'm in the process of developing a feature that automatically updates a player's high score to their account when they achieve a new record. However, I'm struggling with how to search for a player by name and update their tag using mongoose. ...

Adding a C# variable to a URL in a Razor view using jQuery

My Razor page looks like this: @{ ViewData["Title"] = "mypage"; string ApiAddress = "https://localhost:8114/api/"; } <div> ... </div> @section Scripts{ <script> functio ...

Arrange three div elements to create a layout that is optimized for different screen sizes

Is there a way to align these 3 divs as follows: pickup on the left, phone in the middle, and delivery on the right? I've browsed through similar alignment issues on Stack Overflow, but none of the solutions seem to work for my specific code. It' ...

Typescript is throwing an error stating that utilizing 'undefined' as an index type is invalid

When working with TypeScript and React, I pass xs, sm, md, lg as props to the component. Each size corresponds to a specific pixel value (14px, 16px, 18px, 24px) that is then passed to an Icon component. The errors mentioned in point (1) occur at the line ...

Aligning buttons in an inline form with Bootstrap

Issue with Button Alignment Post Validation. <form class="form-inline form-padding"> <div class="form-group"> <div class="input-group"> <input class="form-control" placeholder="First Name" name="fir ...

Ways to apply Angular ng-options outside of a select element besides traditional use?

According to the official documentation, ng-options can be used on any element, not limited to just the select tag. This flexibility allows for enhanced customization of the selection UI, such as displaying thumbnail images or additional information beyond ...

Generating interactive cards using an array

In an attempt to showcase information for three separate months using a single component, I am creating cards titled "Month 1," "Month 2," and "Month 3." Each card contains a table, and I am aiming to add headers to each card based on an array of titles. ...

What could be causing the npm server error in my Vue.js application?

After recently setting up Node.js and Vue.js, I decided to dive into my first project on Vue titled test. As part of the process, I attempted to configure the server using the command: npm run server However, I encountered the following error message: C ...

What is the reason for Python Flask to consume both instances of a file when using .decode()?

In my program, I am fetching a CSV file from an HTML form and decoding it using utf-8. However, I need to have two instances of this file for different purposes. The issue arises when I use .decode('utf-8'), as both instances end up being consume ...

What is the purpose of using the variable "header_row || 1" in App Script?

Recently, I stumbled upon a spreadsheet that contains app script for gathering keys of data requested through doGet. In the code, there is a line that reads like this: var headRow = e.parameter.header_row || 1; What exactly does this line mean? I searc ...

Guide on dynamically loading email contacts options in selectize.js

I have been working with selectize.js to create an email contacts feature. However, I am facing an issue where the mail list retrieved from the database is displaying as undefined in selectize. Strangely, when I manually enter the return value, everything ...

Extracting Node.js data within a Node.js script

Can a node.js file be parsed to execute part of the code within another node.js file? I am attempting to parse a node.js file from within a different script. I do not have control over the contents of the file, but it always contains a function called get ...

Switch up the background picture by chance when hovering over it with the mouse

Would you like to change the background image when hovering over an album, similar to Facebook's functionality? When hovering over an album, display a preview of 3-4 images randomly selected. How can this be achieved using jQuery? I have tried impleme ...

Tips for moving a polygon while dragging a marker within its boundaries

Currently, I have a map displaying polygons and markers, accompanied by a sidebar featuring tool buttons (similar to the setup showcased in this demo: ). Each marker on my map is connected to the respective polygon stored in my database. When I utilize the ...

Three.js - Model is unable to cast shadows

Greetings! I am currently diving into the world of three.js and utilizing react-three-fiber to integrate it with React. However, I've encountered a challenge regarding shadow casting between models. Despite setting obj.castShadow = true and obj.receiv ...

Avoid unnecessary renders by only updating state if it has changed from the previous state

Is there a specific React lifecycle method that can trigger a re-render only when the updated state differs from the previous state? For instance, consider the code snippet below: class App extends Component { constructor() { super(); this.state ...

The issue with the jQuery click event arises when utilizing the "Module Pattern"

Exploring the Module Pattern as described by Chris Coyyer here, I, an intermediate front-end JS developer, have encountered a problem. Specifically, when attempting to utilize a jQuery selector stored in the settings object, I am unable to trigger a click ...