Correct way to define an empty dom element in React

后端 未结 2 697
花落未央
花落未央 2021-02-05 00:19

I send an optional parameter checkbox inside a prop to a component:

var checkBox = this.props.checkbox ?          


        
2条回答
  •  北海茫月
    2021-02-05 01:01

    Use false, null or undefined.

    This snippet shows the behaviour of rendering falsy values in React:

    
    
    
    

    false, null and undefined don't generate anything:

    BeforeAfter
    BeforeAfter
    BeforeAfter

    Whereas '', 0 and NaN do:

    Before0After
    BeforeAfter
    BeforeNaNAfter

    As a convenience. you can conditionally generate content inline if the value you're testing against will be false, null or undefined when the conditional content shouldn't be displayed:

    ... {this.props.checkbox && } ...

提交回复
热议问题