React js Disable button on click
React Js Disable button after click: In React.js, you can disable a button after it is clicked by utilizing the state management feature. First, you would define a state variable, let's say 'disabled', in the component's state using the useState hook. Initially, you would set the 'disabled' state to false. Then, in the button's click event handler function, you would set the 'disabled' state to true using the 'setDisabled' function provided by the useState hook. This would trigger a re-render of the component with the button now being disabled. You would also update the 'disabled' attribute of the button in the JSX code to reflect the 'disabled' state value. With this approach, the button would be disabled and unclickable after it is clicked, preventing multiple clicks or unwanted interactions.




Thanks for your feedback!
Your contributions will help us to improve service.
How can I React Js disable Button when it is clicked?
In the Below code, the button will be disabled when isDisabled
state is true
. To set isDisabled
to true
on button click, you can define a function named disableButton
that sets isDisabled
to true
using setIsDisabled
method from the useState
hook. This function should be passed to the onClick
event of the button.
Here's an example code snippet that demonstrates how to disable the button when it is clicked using React JS:
React Js Disable button on click Example
xxxxxxxxxx
<script type="text/babel">
const { useState } = React;
function App() {
const [isDisabled, setIsDisabled] = useState(false);
const disableButton = () => {
setIsDisabled(true);
};
return (
<div>
<h3>React js disable button on click</h3>
<button disabled={isDisabled} onClick={disableButton}>
Send
</button>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>