React Curriculum

Conditional Rendering & List Keys

Medium100% Free~25 mins#react#conditional-rendering#keys#list-rendering#falsy-zero#reconciliation
Interview Answer (30-Second Summary)

Conditional rendering displays UI based on conditions, where the falsy zero (0 && <Component />) pitfall renders the literal number 0. Keys give list elements stable identities across renders, allowing React to match existing DOM nodes instead of re-creating or incorrectly recycling stateful elements.

Key Learning Objectives
✓

Diagnose the falsy zero trap in JSX short-circuit conditional expressions.

✓

Explain how React reconciles lists and why index as key causes state corruption on reordering.

✓

Understand how changing a key forces React to reset component state and remount.

✓

Formulate deterministic keys from stable data IDs.

The Interview Problem & Context

What is rendered to the DOM when the following component evaluates a zero count condition and reverses a list of stateful items indexed by array index?

1// Simulation of React rendering semantics
2function simulateRender() {
3 const count = 0;
4 // Bug trap: 0 && <span /> evaluates to 0, which React renders as text!
5 const conditionalOutput = count && 'Items Available';
6
7 const hasItems = count > 0;
8 const safeOutput = hasItems && 'Items Available';
9
10 // List keyed with stable IDs: 'A', 'B', 'C' -> reversed to 'C', 'B', 'A'
11 const items = ['A', 'B', 'C'];
12 const reversed = [...items].reverse();
13 const listKeys = reversed.join('-');
14
15 return `${conditionalOutput} ${Boolean(safeOutput)} ${listKeys}`;
16}
17
18console.log(simulateRender());
Predict Console & Render Output
A

0 false C-B-A

B

null false C-B-A

C

false false A-B-C

D

0 true C-B-A

Execution Trace
Step 1 of 4 (Line 3)

count is set to 0.

Component / Call Stack
Global Context
simulateRender()
Hook State & Scope
count:0
Console Output(No console logs emitted)
Deep Architectural Explanation

The Two Classic React Rendering Traps

1. The Falsy Zero Trap (0 && <Component />)

In JSX:

  • false, null, undefined, and true are valid children that render nothing.
  • The number 0 is a valid child that renders "0" on the screen!
tsx
{count && <CartBadge count={count} />} {/* Renders "0" when count is 0! */}
{count > 0 && <CartBadge count={count} />} {/* Correct: renders nothing when 0 */}

2. Why Index as Key Breaks State

React uses the key prop to match children between render passes. If you use the array index as the key:

  1. You insert an item at the beginning of the list.
  2. The new item gets key 0, the previous item gets key 1.
  3. React thinks key 0 is the same component that existed before, preserving internal state (like uncontrolled input values or focus) on the wrong item!
  4. Always use stable, unique data IDs (e.g., item.id) as keys.
Common Interview Traps & Pitfalls
✕

Writing {items.length && <List />} which displays 0 when the list is empty.

✕

Using Math.random() as a key, causing components to remount and lose state on every single render.

✕

Using array index as key in dynamic, filterable, or reorderable lists.

Senior Interviewer Follow-Up Probes
?

Why does changing the key prop of a component force React to completely unmount and remount it?

?

How does React reconcile items when keys are swapped vs when elements have no keys at all?

?

Can you use index as key if the list is purely static and never filtered or reordered?

Frequently Asked Interview Questions
Why does React render 0 on the screen when using count && <Component />?

In JavaScript, logical AND short-circuits to the first falsy operand (0). While React suppresses booleans and null, it treats numbers as renderable text, printing "0".

Keys give elements a stable identity between renders, allowing React reconciliation to determine which items were added, removed, or moved rather than recreating the entire list.

When items are inserted, deleted, or reordered, keys shift. React associates component state (such as input text, checkboxes, or animations) with the key index, causing state to attach to the wrong data item.

Yes, only if the list is completely static: items never reorder, filter, add, or delete, and list items have no internal state.

Giving a component a key based on an ID (e.g., <UserProfile key={userId} />) causes React to unmount the old instance and mount a fresh component whenever the ID changes, cleanly resetting all state.