Skip to main content

rendering-conditional-render

Use explicit ternary operators instead of && for conditional rendering. Apply when rendering values that could be 0, NaN, or other falsy values that might render unexpectedly.

来源信息

仓库
TheOrcDev/8bitcn-ui
最近来源活动
2026年1月16日 09:09
检测到的 SKILL.md 语言
英语
星标
2,060
Forks
119

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。

正在显示 SKILL.md

SKILL.md
来源说明 · 只读预览
name
rendering-conditional-render
description
Use explicit ternary operators instead of && for conditional rendering. Apply when rendering values that could be 0, NaN, or other falsy values that might render unexpectedly.
## Use Explicit Conditional Rendering Use explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be `0`, `NaN`, or other falsy values that render. **Incorrect (renders "0" when count is 0):** ```tsx function Badge({ count }: { count: number }) { return ( <div> {count && <span className="badge">{count}</span>} </div> ) } // When count = 0, renders: <div>0</div> // When count = 5, renders: <div><span class="badge">5</span></div> ``` **Correct (renders nothing when count is 0):** ```tsx function Badge({ count }: { count: number }) { return ( <div> {count > 0 ? <span className="badge">{count}</span> : null} </div> ) } // When count = 0, renders: <div></div> // When count = 5, renders: <div><span class="badge">5</span></div> ```
在 GitHub 查看