How to disable input field on button click in react?
May 26, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
Today, I am going to show you. How to disable input field on button click in react with code example.
If we want to disable
our button after clicking on it, We can disable it by using react’s state. We will set the button’s disable
state to false
on load and add the onClick
function in our button element, which will set the button’s disable
state to true
. So, When a user clicks on the button, it will change its disable
state to true
.
Let’s look at the following example to understand how it basically works:
APP.js
import React from "react";
function DisableAfterClick() {
const [disable, setDisable] = React.useState(false);
return (
<button disabled={disable} onClick={() => setDisable(true)}>
Click to Disable!
</button>
);
}
export default DisableAfterClick
Check the output of the above code example.
All the best 👍