Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic disabled input

Tags:

reactjs

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?

like image 503
asjjkhbk Avatar asked Dec 03 '25 05:12

asjjkhbk


2 Answers

<input disabled={isDisabled ? true : false}>

You have to use like this It's working.

like image 182
Jay Parmar Avatar answered Dec 04 '25 18:12

Jay Parmar


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>
    </>
  );
};

Edit stupefied-borg-5zc1r

like image 24
Dennis Vash Avatar answered Dec 04 '25 20:12

Dennis Vash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!