在 React 中我可以做这样的事情吗:
style={ $post.type === "team_member"?背景颜色:“绿色”
我如何根据 React 中的条件设置样式?
提前致谢!
最佳答案
你可以,而且接近你已有的;
style={{backgroundColor: $post.type === "team_member" ? 'green': 'not_a_team_member'}}
style
属性需要一个对象,因此是双 {}
,您只是有条件地分配 backgroundColor
的值,因此conditional 是对象中键 backgroundColor
的值。
希望这对您有所帮助!
关于javascript - React 中的条件样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53435184/