I have this element:
<div id="app-wrapper" style={{"grid-template-columns": "50% 50%"}}>..</div>
I want to based on a condition defined in my component's state change the style property to this:
<div id="app-wrapper" style={{"grid-template-columns": "100%"}}>..</div>
You can conditionally render the style using a ternary operator in 2 ways:
Option 1:
<div id="app-wrapper" style={state? {"grid-template-columns": "50% 50%"} : {"grid-template-columns": "100%"}}>..</div>
Option 2 (cleaner):
const divStyle = state? {"grid-template-columns": "50% 50%"} : {"grid-template-columns": "100%"}
<div id="app-wrapper" style={divStyle}>..</div>
您喜欢使用三元运算符:
<div id="app-wrapper" style={{"grid-template-columns": condition?"100%":"50% 50%"}}>..</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.