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
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers
NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
What is an API (Application Programming Interface)
In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network
A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read