How to Disable a Button in React JS ?
Last Updated :
26 Jul, 2024
Disabling a button in React JS means making it unclickable and visually indicating its unavailability. This is done by setting the button's disabled attribute to true. It's used for controlled interactions, like form submissions with valid data.
A disabled button becomes unclickable and visually signifieÂs to the user that its functionality is currently unavailableÂ. This common practice helps prevent users from performing invalid or prohibited actions within a specific context.
Syntax:
<button disabled={true}>Disabled Button</button>
Prerequisites:
These are the approaches to disable a button in React JS
Steps to Create React Application:
Step 1: Create a react application by using this command
npx create-react-app disable-button-app
Step 2: After creating your project folder, i.e. disable-button-app, use the following command to navigate to it:
cd disable-button-app
Project Structure:

Approach 1: State Management with Alert Message
The approach involveÂs using React's state hooks to manage button disableÂd states. Two functions are createÂd for toggling the disabled state, accompanieÂd by alerts to provide user massage. The buttons' appearance is determined by conditional internal styling. The internal CSS defineÂs a centered layout with a promineÂnt green heading and distinct styleÂs for disabled (gray, no cursor) and enabled (reÂd, pointer cursor) button states.
Example: This creates a component that displays two buttons, "Disable" and "Enable." Clicking "Disable" disables the "Disable" button and vice versa. Alert messages confirm the actions.
JavaScript
import React, { useState } from 'react';
function App() {
const [isButtonDisabled, setButtonDisabled] = useState(false);
const disableButton = () => {
setButtonDisabled(true);
alert("Button has been disabled!");
};
const enableButton = () => {
setButtonDisabled(false);
alert("Button has been enabled!");
};
return (
<div style={styles.container}>
<h1 style={styles.heading}>Geekforgeeks</h1>
<button
onClick={disableButton}
style={isButtonDisabled ?
styles.disabledButton : styles.enabledButton}
disabled={isButtonDisabled}
>
Disable
</button>
<button
onClick={enableButton}
style={!isButtonDisabled ?
styles.disabledButton : styles.enabledButton}
disabled={!isButtonDisabled}
>
Enable
</button>
</div>
);
}
export default App;
const styles = {
container: {
textAlign: 'center',
margin: 'auto',
padding: '20px',
width: 400,
},
heading: {
fontSize: '34px',
marginBottom: '10px',
color: "green",
borderBottom: "3px solid green",
paddingBottom: 20,
borderRadius: "8px",
},
disabledButton: {
backgroundColor: 'gray',
color: 'white',
cursor: 'not-allowed',
margin: 10,
padding: 15,
borderRadius: "8px",
border: "none",
boxShadow: "0px 0px 10px 0px grey",
},
enabledButton: {
backgroundColor: 'red',
color: 'white',
cursor: 'pointer',
margin: 10,
padding: 15,
borderRadius: "8px",
border: "none",
boxShadow: "0px 0px 10px 0px grey",
},
};
Steps to Run the Application: open the application, use the Terminal and enter the command below.
npm start
Output: This output will be visible on the http://localhost:3000/ on the browser window.
Approach 2: Conditional Rendering
Conditional rendering in React involves rendering components based on conditions, enabling or disabling UI elements dynamically. Useful for managing component visibility and interactions.
Example: This example creates a form interface with an input field and a submit button. The button is initially disabled when the input field is empty. Once the user enters text, the button becomes enabled, allowing them to submit. Clicking the button triggers an alert showing the input value.
JavaScript
import React, { useState } from 'react';
function App() {
const [inputValue, setInputValue] = useState('');
const handleSubmit = () => {
if (inputValue.length > 0) {
alert("Form submitted with value: " + inputValue);
}
};
return (
<div style={styles.container}>
<h1 style={styles.heading}>Geekforgeeks</h1>
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
style={styles.input}
/>
<button
onClick={handleSubmit}
style={inputValue.length === 0
? styles.disabledButton : styles.enabledButton}
disabled={inputValue.length === 0}
>
Submit
</button>
</div>
);
}
export default App;
const styles = {
container: {
textAlign: 'center',
margin: 'auto',
padding: '20px',
},
heading: {
fontSize: '34px',
marginBottom: '10px',
color: "green",
borderBottom: "3px solid green",
paddingBottom: 20,
borderRadius: "8px",
},
input: {
padding: '10px',
marginBottom: '10px',
width: '200px',
borderRadius: 8,
},
disabledButton: {
backgroundColor: 'gray',
color: 'white',
cursor: 'not-allowed',
margin: 10,
padding: 15,
borderRadius: "8px",
border: "none",
boxShadow: "0px 0px 10px 0px grey",
},
enabledButton: {
backgroundColor: 'green',
color: 'white',
cursor: 'pointer',
margin: 10,
padding: 15,
borderRadius: "8px",
border: "none",
boxShadow: "0px 0px 10px 0px grey",
},
};
Steps to Run the Application: open the application, use the Terminal and enter the command below.
npm start
Output: This output will be visible on the http://localhost:3000/ on the browser window.
Similar Reads
How to Disable a Button in jQuery UI ?
To disable a button in jQuery UI, we will be using disable() method which is discussed below: jQuery UI disable() method is used to completely disable the button. It returns the button element completely to its initial state. Syntax: $(".selector").button("disable") Parameters: This method does not
2 min read
How to Disable Radio Button in HTML?
This article will show you how to disable Radio Button in HTML. The disabled attribute is used to disable a radio button in HTML. This attribute prevents the radio button from being selected by the user. Using the disabled AttributeThe disabled attribute is used to disable the input fields. To disab
2 min read
How to Create Button in React-Native App ?
React Native provides pre-defined components like button and TouchableOpacity to create buttons in a react native app. In this article, we will see how to create buttons in react-native, their syntax, and different types of buttons available in react-native.Table of ContentApproachButton in React Na
4 min read
How to Disable a Button Conditionally in JavaScript?
In JavaScript, you can conditionally disable buttons based on specific criteria to control user interactions. Using simple if-else statements or event listeners, you can dynamically enable or disable buttons depending on form validation, user actions, or other conditions. This helps regulate the ava
3 min read
React Suite Button Disabled
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. The Button component is used to fire an action when the user clicks the button. In this article, we will be seeing React Suite Button Disabled. The disabled pro
3 min read
How to create a basic button in React Native ?
In this article, we will learn how to create a basic button in React Native. To build a React Native app, we will use the Expo CLI, which provides a smoother experience for running your applications.ExpoIt is a framework and a platform for universal React applications. It is a set of tools and servi
5 min read
How to Create a Upload File Button in ReactJS?
File upload button is a commen feature in React Apps used to take the file input from user and process it. To perform this we can simple use input type file and trigger the input event with the help of upload button.ApproachTo create a upload file button in React we will be using the input type file
3 min read
How to add Floating Buttons in React Native ?
React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, macOS, tvOS, Web, Windows, and UWP by enabling developers to use the React framework along with native platform capabilities.In this article, we will
2 min read
How To Enable Button Based On If statement in ReactJS?
React.js is one of the most popular JavaScript libraries for building user interfaces, and one common task is enabling or disabling a button based on certain conditions or user inputs. This is often achieved by using conditional logic such as if statements.In this article, we'll explore how to enabl
2 min read
How to disable radio button using JavaScript ?
In HTML, we have a feature through which we can select one option out of multiple given options, and we can do that with the help of the Radio button. So, In this article, we will learn how we can disable these radio buttons for some circumstances. we will see how radio buttons work and why we need
5 min read