How to disable a Button in React

avatar
Borislav Hadzhiev

Last updated: Apr 7, 2024
3 min

banner

# Disable a button in React

Use the disabled prop to disable a button in React.

You can use the prop to conditionally disable the button based on the value of an input field or another variable or to prevent multiple clicks to the button.

App.js
import {useState} from 'react'; export default function App() { const [message, setMessage] = useState(''); const isAnonymous = true; const handleClick = event => { event.currentTarget.disabled = true; console.log('button clicked'); }; return ( <div> {/* ✅ Disable button when input is empty */} <div> <input type="text" id="message" name="message" value={message} onChange={event => setMessage(event.target.value)} /> <button disabled={!message}>Click</button> </div> <hr /> {/* ✅ Disable button */} <button disabled={true}>Click</button> <hr /> {/* ✅ Disable button conditionally */} <button disabled={isAnonymous ? true : false}>Click</button> <hr /> {/* ✅ Disable a button after it has been clicked once */} <button onClick={handleClick}>Click</button> </div> ); }

react disable button

The code for this article is available on GitHub

The first example shows how to disable a button when an input field is empty.

App.js
{/* ✅ Disable button when input is empty */} <div> <input type="text" id="message" name="message" value={message} onChange={event => setMessage(event.target.value)} /> <button disabled={!message}>Click</button> </div>

We set the onChange prop on the input element, so every time its value changes, the supplied function gets invoked.

We used the useState hook to store the value of the input.

App.js
const [message, setMessage] = useState('');

To disable a button in React, we have to set the disabled prop on the element.

App.js
<button disabled={!message}>Click</button>

The example uses the logical NOT (!) operator to negate the value of the message variable.

In other words, if the message variable stores an empty string, which is a falsy value, we set the disabled prop to true and disable the button.

All other string values are truthy, so negating a truthy value sets the button's disabled prop to false.

If you need to disable a link, follow the instructions in my How to disable a Link in React article.

# Conditionally disable a button in React

You can also use conditionals to determine whether a button should be disabled.

App.js
<button disabled={isAnonymous ? true : false}>Click</button>
The code for this article is available on GitHub

The example uses the ternary operator which is very similar to an if/else statement.

The expression before the question mark gets evaluated and if it returns a truthy value, the operator returns the value before the colon, otherwise, it returns the value after the colon.

In other words, if the isAnonymous variable stores a truthy value, we return true and disable the button, otherwise, we return false which leaves the button enabled.

If you need to check if an input is empty, click on the following article.

# Disable a button after the first click

The last example shows how to prevent multiple clicks on a button by disabling the button after the first click.

App.js
export default function App() { const handleClick = event => { event.currentTarget.disabled = true; console.log('button clicked'); }; return ( <div> {/* ✅ Disable the button after it has been clicked once */} <button onClick={handleClick}>Click</button> </div> ); }

react prevent multiple button clicks

The code for this article is available on GitHub

We set an onClick prop on the button element. When the button gets clicked, the handleClick function is invoked.

We used the currentTarget property on the event to get a reference to the button and set its disabled attribute to true.

The currentTarget property on the event gives us access to the element that the event listener is attached to.

Whereas the target property on the event gives us a reference to the element that triggered the event (could be a descendant).

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.