Mateusz
Mateusz

Reputation: 97

Can I add new class inside the same element using jsx?

What I mean, is there a way to write sth like this?

<input
        className="form-control"
        onChange={() => { 
          this.classList.add('is-valid');  //where this is input element
        }}
      />

Upvotes: 0

Views: 33

Answers (1)

T J
T J

Reputation: 43156

You can use a local state (useState) and conditional expression as shown below:

function App() {

  const [isValid, setIsValid] = useState(false);

  return (<input
    className={'form-control' + (isValid ? ' is-valid' : '')}
    onChange={() => {
      setIsValid(true);
    }}
  />)
}

Upvotes: 2

Related Questions