I want to make an input field disabled on the basis of prop variable.
I am doing like this:
<input {isDisabled ? 'disabled' : ''}>
But this is not working. Then how can I do that?
<input disabled={isDisabled ? true : false}>
You have to use like this It's working.
You need to use the disabled prop:
<input disabled={isDisabled}/>
// Same but less readable
<input disabled={isDisabled ? true : false}/>
<input {...{disabled: isDisabled}}/>
For example:
const App = () => {
const [isDisabled, toggle] = useReducer(p => !p, false);
return (
<>
<input disabled={isDisabled} />
<button onClick={toggle}>Toggle</button>
</>
);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With