Under The Hood
frontend solidjs SolidJS 1.x / 2.x

<For> vs <Index>: Keyed by Item Reference vs Keyed by Array Index

Last updated
Prerequisites:
SolidJS Components and JSX
Array manipulation in signals
  • solidjs
  • control-flow
  • For
  • Index
  • lists
  • performance

Read at your depth

The practical view

Solid provides two built-in list components: `<For>` and `<Index>`. `<For each={items()}>` **keys by object reference**: when items move, reorder, or are inserted, the DOM nodes move with the item, and the item passed to the callback is static while its index is a signal (`item, indexSignal`). `<Index each={items()}>` **keys by array index**: DOM nodes stay fixed at their indices, while the item passed to the callback is a signal (`itemSignal, indexInt`).

The same idea in other frameworks

react equivalent
`{items.map((item, index) => <Item key={item.id} />)}` (equivalent to `<For>`).

Legacy vs modern

When to Use <For> vs <Index>

Use `<For>` for lists of objects where element identity matters; use `<Index>` for arrays of primitives to avoid DOM node re-allocations.

before → after
Wrong Choice: <For> on Primitive Arrays
<For each={numbers()}>{(num) => <input value={num} />}</For> // ❌ Re-creates DOM node on every edit
Correct Choice: <Index> for Primitives
<Index each={numbers()}>{(numSignal, index) => <input value={numSignal()} />}</Index> // ✅ NEW: Preserves DOM & focus

Interview gotchas

Context

Interviewers test your practical attention to detail regarding signal parameters.

The mechanical answer

In `<For each={items}>{(item, index) => ...}</For>`, **`item` is a static value** and **`index` is a reactive accessor signal (`index()`)**. In `<Index each={items}>{(item, index) => ...}</Index>`, **`item` is a reactive accessor signal (`item()`)** and **`index` is a static number**.

Trap

Calling `item()` in a `<For>` loop will cause a `TypeError: item is not a function`. Omitting `()` on `item` in an `<Index>` loop will render the raw function representation instead of the value.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.