In React can I do something like this:
style={ $post.type === "team_member" ? backgroundColor : "green"}
How can I style according to a condition in React?
Thank you in advance!
In React can I do something like this:
style={ $post.type === "team_member" ? backgroundColor : "green"}
How can I style according to a condition in React?
Thank you in advance!
You can, and it's close to what you already have;
style={{backgroundColor: $post.type === "team_member" ? 'green': 'not_a_team_member'}}
The style
attribute expects an object, hence the double {}
, you're only assigning the value of backgroundColor
conditionally, so the conditional is the value of the key backgroundColor
in the object.
Hope this helps!
You can do something like this:
<div style={{ visibility: this.state.post.type === 'team_member'? 'green': ''}}></div>
You can do something like this:
<div style={ post.type === "team_member" ? {backgroundColor : "green" }: '' }}></div>