Rendering Patterns

Rendering strategy affects both correctness and performance in production React applications.

Conditional rendering

Use standard JavaScript conditions inside JSX to switch between loading, error, empty, and ready states. Prefer ternary expressions (cond ? a : b) or early returns over count && <Badge />, because a falsy 0 with && renders the literal digit 0 on screen:

function StatusView({ isLoading, error, items }) {
  if (error) return <p role="alert">{error}</p>;
  return isLoading ? <Spinner /> : <Dashboard items={items} />;
}

Lists and keys

Render arrays with .map() and attach a unique, stable key prop to the outermost element returned by the callback. React uses key across renders to match existing DOM nodes and component state when items are inserted, reordered, or deleted:

{items.map(item => (
  <Row key={item.id} item={item} />
))}

Fragments and composition

Use <>...</> (or <React.Fragment key={...}> inside lists) to group sibling elements without injecting extra <div> wrappers that break CSS Grid, Flexbox, or HTML table semantics.

Common pitfalls

Watch for these subtle bugs that cause flickering state or sluggish updates:

  • Array index as key: When items are sorted, filtered, or prepended, index keys bind old component state (such as input text) to the wrong row.
  • Heavy work in render: Sorting or filtering thousands of items on every keystroke slows typing; derive or memoize expensive transforms with useMemo only when profiling shows a bottleneck.

Professional tips

Keep rendering predictable and easy to debug:

  1. Use stable database or domain IDs for key props.
  2. Keep render functions pure—never trigger network requests or mutate external variables during render.
  3. Introduce React.memo or useMemo only after profiling evidence, not by default.

Practice

Implement a filterable task table with conditional empty/error/loading states and stable-key list rendering.