Deactivate input in react with a button click
98,849
Solution 1
A simplified solution using state could look like this:
class Typing extends React.Component {
constructor(props) {
super(props);
this.state = { disabled: false }
}
handleGameClik() {
this.setState( {disabled: !this.state.disabled} )
}
render() {
return(
<div>
<input
className = "typing-container"
placeholder= " type here "
disabled = {(this.state.disabled)? "disabled" : ""}/>
<button onClick = {this.handleGameClik.bind(this)}> Start Game </button>
<button> Fetch Data </button>
</div>
);
}
};
Working Codepen here.
Solution 2
** 2019 **
Another option is to use, react-hooks' hook useState
.
Edit: In a functional component
import React, {useState} from 'react';
function Typing(props) {
const [disabled, setDisabled] = useState(false);
function handleGameClick() {
setDisabled(!disabled);
}
return (
<div>
<input
className='typing-container'
placeholder=' type here '
disabled={disabled}
/>
<button type='submit' onClick={handleGameClick}> Start Game </button>
<button> Fetch Data </button>
</div>
);
}
Solution 3
This might confuse you, but the guys at React.js actually rebuild every form component and made them look almost exactly like the native HTML component. There are some differences however.
In HTML you should disable an input field like this:
<input disabled="disabled" />
But in React.js you'll have to use:
<input disabled={true} />
The accepted example works because anything not 0
is considered true
. Therefor "disabled"
is also interpreted as true
.
Related videos on Youtube
Comments
-
Second Son almost 2 years
I have this basic component and I want the textfield to be deactivated or activated whenever I click on a button. How can I achieve this?
This is my sample code:
import React from "react"; import Button from 'react-button' const Typing = (props) => { var disabled = "disabled"; var enabled = !disabled; const handleUserInput = (event) => props.onUserInput(event.target.value); const handleGameClik = (props) => { disabled = enabled; } return( <div> <input className = "typing-container" value = {props.currentInput} onChange = {handleUserInput} placeholder=" ^__^ " disabled = {disabled}/> <Button onClick = {handleGameClik}> Start Game </Button> <Button> Fetch Data </Button> </div> ); };
-
Ali Sepehri.Kh about 8 yearsYou need to store
disable
variable in componentstate
and change it there. When you change the state of the component, therender
method will invoke and refresh the component.
-
-
Foxhoundn about 8 yearsYou do not need to do
{(this.state.disabled)? "disabled" : ""}
when a prop is false || null it is automatically ommited.disabled={this.state.disabled}
is enough. -
Paul Razvan Berg over 4 yearsOr, if your
disabled
state depends on some other prop or state item, use useMemo. -
Crisan Lucian over 4 yearsreact-hooks' hook -> functional component
-
Domino over 3 yearsBesides, both
disabled="disabled"
anddisabled=""
mean the input is disabled in HTML. Boolean attributes are false when the attribute is removed, not set to an empty string. In fact, this answer should definitely be edited to remove that incorrect code. -
wintvelt over 3 years@Domino I agree that my code could be a bit cleaner/ nicer, but it is not incorrect. you can see in the working codepen that the example code just works, so “disabled” and “” do not mean the same thing in react/jsx. Today, I would probably use hooks and something like
<input disabled={this.state.disabled}
. -
Domino over 3 yearsAh, I see. React doesn't actually call
element.setAttribute("disabled", "")
but probably translates that intoelement.disabled = ""
and the empty string is false. That sure is a quirk to keep in mind. -
Denis over 2 yearsPlease provide additional details in your answer. As it's currently written, it's hard to understand your solution.
-
Sarthak Bansal over 2 yearsThis is a better solution