Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 39 ยท JSX and Control Flow

Lists with For and Index

Render lists in SolidJS with For and Index: reference versus index keying, the index accessor, fallbacks, nested lists and performance tips.

Lists are where Solid's design pays off. Instead of re-rendering a whole array and diffing it, Solid's list components keep one DOM node per item and move, add or remove only the nodes that changed. There are two components, For and Index, and the difference between them is what identifies an item.

For: items keyed by reference

JSX
<ul>
  <For each={todos()} fallback={<li>Nothing to do.</li>}>
    {(todo, i) => <li>{i() + 1}. {todo.title}</li>}
  </For>
</ul>
  • each is the array (or a function returning one).
  • The child is a function called once per item. It receives the item and an index accessor, so call i() to read it.
  • fallback shows when the array is empty.

For tracks items by reference. If you reorder the array, the existing DOM rows for each object are moved, not rebuilt. If an item object is replaced by a new object, that row is rebuilt. Use For for lists of objects, which covers most cases.

For keeps rows when you reorder

Type something into an input, then press reverse: the text stays with its row because the DOM node moved with its object. The console shows "row created" only for new items, never for a reorder.

Index: items keyed by position

Index works the other way around. The DOM node belongs to a position, and the item is a signal that changes.

JSX
<Index each={names()}>
  {(name, i) => <li>{i}: {name()}</li>}
</Index>

Notice the swap: in Index, the item is the accessor (name()) and the index is a plain number (i). If you replace the value at position 2, the node at position 2 stays and its text updates.

Use Index for lists of primitives (strings, numbers) or when you edit values in fixed slots, such as a row of text inputs bound to an array. With For and primitives, changing a value would destroy and recreate the row, and an input inside it would lose focus on every keystroke.

Index: editing primitives in place

You can type in any box without losing focus, because the input at that position is never recreated, only its value signal changes.

Which one?

Your list holds Typical operation Use
Objects with identity (todos, users) add, remove, reorder For
Primitives edited in place change value at a position Index
Primitives that are added/removed/reordered rare, usually fine For

A good rule: For unless the items are primitives you edit in place.

Nested lists, empty states and derived lists

JSX
<For each={groups()}>
  {(group) => (
    <section>
      <h3>{group.title}</h3>
      <ul>
        <For each={group.items}>{(item) => <li>{item}</li>}</For>
      </ul>
    </section>
  )}
</For>

For a filtered or sorted view, derive it with a memo and pass that to each, so the work happens once per change:

JavaScript
const visible = createMemo(() => todos().filter((t) => !t.done));
// <For each={visible()}>

For large reactive arrays of objects, store them in a store (see Stores with createStore), where editing one field updates only the affected cell and For rows are not recreated.

Why not .map()?

{items().map(i => <li>{i}</li>)} is valid, but when items() changes, the entire expression runs again and every <li> is recreated. For reconciles instead: unchanged rows survive and updates are targeted. The same goes for filter().map() chains used for rendering.

Common mistakes

  • Mixing up the two signatures. In For, the index is the accessor; in Index, the item is the accessor.
  • Using For with primitives you edit, which recreates rows and drops input focus.
  • Calling i without parentheses in For (it is a function), or calling it in Index (it is a number).
  • Using .map() for dynamic data, losing the efficient updates.
  • Mutating the array in place and calling the setter with the same array: the signal sees the same reference and does not notify. Create a new array, or use a store.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In <For>, what identifies a row?

    For keys by reference, so reordering moves existing rows.
  2. 2. In <Index>, what does the callback receive as its first argument?

    Index rows are fixed to positions; the item is a signal-like accessor and the index is a number.
  3. 3. Which list type is best served by Index?

    With primitives edited in place, Index keeps each input and updates its value.
  4. 4. Why is items().map(...) less efficient than For?

    The whole expression re-runs on change; For reconciles by item.

Summary

  • For keys rows by object reference and gives an index accessor: the default choice.
  • Index keys rows by position and gives an item accessor: for primitives edited in place.
  • Add fallback for empty lists, derive filtered lists with memos.
  • Avoid .map() for changing lists; replace arrays instead of mutating them.

Next: Dynamic, Portal and ErrorBoundary