Correct way to handle conditional styling in React

前端 未结 9 1241
逝去的感伤
逝去的感伤 2020-11-27 04:25

I\'m doing some React right now and I was wondering if there is a \"correct\" way to do conditional styling. In the tutorial they use

style={{
  textDecorati         


        
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 04:54

    If you need to conditionally apply inline styles (apply all or nothing) then this notation also works:

    style={ someCondition ? { textAlign:'center', paddingTop: '50%'} : {}}
    

    In case 'someCondition' not fulfilled then you pass empty object.

提交回复
热议问题