Skip to main content
rule CX Codex

rendering-conditional-render — Claude Code rule

The rendering-conditional-render rule prevents accidental rendering of zeros and NaN in JSX. It enforces explicit ternary operators instead of && checks, so falsy values stay out of the DOM.

MIT ★ 7,191 updated 2026-10-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/rendering-conditional-render.md
Open on GitHub

What rendering-conditional-render does

In React, count && <Badge>{count}</Badge> looks clean but breaks when count is zero—the number renders as text. The rule requires an explicit check: count > 0 ? <Badge>{count}</Badge> : null. This applies to any numeric or boolean value that could be falsy yet meaningful to your logic.

The rule activates in Claude Code when working with JSX. It scans for && patterns in conditional rendering and suggests rewriting with a ternary operator that explicitly returns null or false.

What it helps with

  • Stop zeros from appearing in badge and counter components
  • Control conditional rendering when props hold numeric values
  • Catch falsy values that should remain invisible in output

Example prompts for the agent

  • See {count && {count}} and let Claude rewrite it with a ternary
  • Review a Badge component that accepts a prop that might be zero
  • Verify that NaN and 0 never leak into the DOM

How to install rendering-conditional-render

Manually: put the files in place

.claude/rules/rendering-conditional-render.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about rendering-conditional-render

How do I install rendering-conditional-render in Claude Code?

Create .claude/rules/rendering-conditional-render.md at your project root with the rule content. Claude Code will pick it up automatically in your next session.

What does this rule do?

It catches conditional render patterns where && is used with numbers or other falsy values. It suggests switching to a ternary operator so that zero or NaN do not reach the output.

Why is count && <Badge> problematic?

The && operator returns its left operand if falsy. When count is zero, React renders the digit 0 directly into the DOM instead of the component.

Source text

rendering-conditional-render.md · MIT
---
title: Use Explicit Conditional Rendering
impact: LOW
impactDescription: prevents rendering 0 or NaN
tags: rendering, conditional, jsx, falsy-values
---

## 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>
```

© tech-leads-club · MIT · tech-leads-club/agent-skills

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
bundle-conditional The bundle-conditional rule recommends loading large modules only when their feature is activated. It applies to lazy-loaded data and browser-only code in SSR builds. CX Codex +1 ★ 7,191repo stars react19-no-forwardref react19-no-forwardref is a Claude Code rule covering React 19 ref and context changes: pass ref as a prop and use use() instead of useContext(). It supports React 19 migrations. CX Codex +1 ★ 7,191repo stars advanced-init-once advanced-init-once prevents running app-wide startup logic inside component useEffect([]) hooks and suggests module-level guards or top-level initialization instead. CX Codex +1 ★ 7,191repo stars rerender-defer-reads The rerender-defer-reads rule keeps callback-only reads of dynamic state out of component subscriptions. Use it when searchParams or localStorage is needed only after an action. CX Codex +1 ★ 7,191repo stars server-serialization server-serialization is a Claude Code rule for limiting data passed across React Server/Client boundaries. It applies when client components receive props from server components. CX Codex +1 ★ 7,191repo stars rerender-simple-expression-in-memo The rerender-simple-expression-in-memo rule advises against using useMemo for simple expressions that return a boolean, number, or string. It applies when reviewing React component calculations. CX Codex +1 ★ 7,191repo stars js-hoist-regexp js-hoist-regexp — a Claude rule that keeps RegExp construction out of the render path: define the pattern at module scope or wrap it in useMemo(). CX Codex +1 ★ 7,191repo stars rendering-hoist-jsx rendering-hoist-jsx extracts static JSX elements outside components to prevent element re-creation on every render pass. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open