Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add optional attributes to an element in React?

Currently, in my React app I have this,

<input style={{ borderColor: valid ? null : "red", boxShadow: valid ? null : "rgb(2, 0, 0)"}} />

If I have more styling to do, it doesn't make sense checking the same condition again and again right? I wish to make the entire style attribute optional; something like this,

<input valid ? null : style={{ borderColor: "red", boxShadow: "rgb(2, 0, 0)"}} />

But I know that's not the right syntax. Isn't there a better way to do this?

like image 375
Karthik G M Avatar asked Dec 11 '25 17:12

Karthik G M


1 Answers

You should use className to add style classes conditionally.You can use the classnames package as answered to the question

like image 51
DevLoverUmar Avatar answered Dec 13 '25 06:12

DevLoverUmar