| 1 | --- |
| 2 | title: Use Explicit Conditional Rendering |
| 3 | impact: LOW |
| 4 | impactDescription: prevents rendering 0 or NaN |
| 5 | tags: rendering, conditional, jsx, falsy-values |
| 6 | --- |
| 7 | |
| 8 | ## Use Explicit Conditional Rendering |
| 9 | |
| 10 | Use explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be |
| 11 | |
| 12 | **Incorrect (renders "0" when count is 0):** |
| 13 | |
| 14 | ```tsx |
| 15 | function Badge({ count |
| 16 | return <div>{ |
| 17 | } |
| 18 | |
| 19 | // When count = 0, renders: <div>0</div> |
| 20 | // When count = 5, renders: <div><span class="badge">5</span></div> |
| 21 | ``` |
| 22 | |
| 23 | **Correct (renders nothing when count is 0):** |
| 24 | |
| 25 | ```tsx |
| 26 | function Badge({ count |
| 27 | return <div>{ |
| 28 | } |
| 29 | |
| 30 | // When count = 0, renders: <div></div> |
| 31 | // When count = 5, renders: <div><span class="badge">5</span></div> |
| 32 | ``` |