The books on the shelf aren't written out one by one. The data sits in an array, and the page has to make one row for each book. If you come from React, you're used to .map() with a key on every row. In Solid, .map() works too, but there are two built-in components that fit better: <For> and <Index>. Both take an array through the each prop and a function that turns each item into an element.
The difference is how they recognise a row: <For> recognises a row by its item, <Index> by its position. Say Nadia sticks a small "gift" note on The Rainbow Troops. The For way: the note is stuck on the book, so when the books are rearranged, the note moves along with The Rainbow Troops. The Index way: the note is stuck on shelf slot 1, so when the books are rearranged, the note stays at slot 1, next to another book.
For: one row per item
import { createSignal, For } from "solid-js"
function App() {
const [fruits] = createSignal(["Apple", "Mango", "Orange"])
return (
<ul>
<For each={fruits()}>
{(item) => <li>{item}</li>}
</For>
</ul>
)
}
The result is one <ul> holding <li>Apple</li>, <li>Mango</li>, <li>Orange</li>, in the data's order.
Foris imported from"solid-js".each={fruits()}is the array. The signal is called with parentheses.- The content of
<For>is a function:(item) => <li>{item}</li>. Solid calls it once per item, anditemis the value itself, so it's written{item}without parentheses. - The second parameter is the position number, as a getter:
(item, i) => <li>{i() + 1}. {item}</li>gives1. Apple,2. Mango,3. Orange. - If the array is empty,
Fordraws nothing. For a search with no results, fill thefallbackprop:<For each={results()} fallback={<li>No book found</li>}>.
fruits().map(item => <li>{item}</li>) shows the same, but every time the array changes, .map() makes every row again. <For> only makes rows for new items. The first exercise checks that the rows come from <For>.
Index: one row per position
import { createSignal, Index } from "solid-js"
function App() {
const [numbers] = createSignal([10, 20, 30])
return (
<ul>
<Index each={numbers()}>
{(num, i) => <li>{i + 1}. {num()}</li>}
</Index>
</ul>
)
}
The result is 1. 10, 2. 20, 3. 30. The roles of the two parameters are the reverse of For:
<For> |
<Index> |
|
|---|---|---|
| A row is recognised by | its item (the same object) | its position (which place it holds) |
| First parameter | a value: item |
a getter: num() |
| Second parameter | a getter: i() |
a plain number: i |
| Fits | lists of objects that get sorted, added to, removed from | lists whose positions stay but whose contents change |
That's the second exercise. If the number at position 2 changes from 20 to 25, Index only replaces the text in row 2. For makes a new row, because 25 is an item it has never seen.
For vs Index when the order changes
Nadia's Bookshop's book list, with a note box in every row:
import { createSignal, For } from "solid-js"
function App() {
const [books, setBooks] = createSignal([
{ title: "The Rainbow Troops", price: 18 },
{ title: "This Earth of Mankind", price: 24 },
{ title: "Beauty Is a Wound", price: 19 },
])
const reverseOrder = () => setBooks(b => [...b].reverse())
const discount = () => setBooks(b => b.map(x => x.title === "The Rainbow Troops" ? { ...x, price: 16 } : x))
return (
<div>
<button onClick={reverseOrder}>Reverse order</button>
<button onClick={discount}>Discount The Rainbow Troops</button>
<ul>
<For each={books()}>
{(b, i) => (
<li>
{i() + 1}. {b.title} · ${b.price}
<input placeholder="notes" />
</li>
)}
</For>
</ul>
</div>
)
}
The Index version is the same, with <Index each={books()}>, {i + 1}, {b().title}, and {b().price}. Type gift in the row for The Rainbow Troops, then click Reverse order. Tested in Solid 1.9:
<For> |
<Index> |
|
|---|---|---|
| Text after the reversal | 1. Beauty Is a Wound, 2. This Earth of Mankind, 3. The Rainbow Troops |
the same |
The <li> elements |
moved, along with their book | stay in place, their text replaced |
The gift note |
in the row for The Rainbow Troops, now number 3 | in row 1, next to Beauty Is a Wound |
| Rows made anew | 0 | 0 |
With Index, gift stays in the first row, and is now next to Beauty Is a Wound. The rows never move, only the text inside them is replaced.
One more thing about For: it recognises an item by its object, not by what's in it. The Discount The Rainbow Troops button swaps its object for a new one, { ...x, price: 16 }, and For takes it as a new item: its row is made again and the gift note is gone. Index only replaces the price text with $16.
⚠️ Swapped parentheses and hand-typed rows
- Mixing up the parentheses. In
For, the item is a value:{b().title}inForstops withTypeError: b is not a function. The position is a getter:{i + 1}inForshowsfunction () { [native code] }1. InIndexit's the reverse:{num()}and{i + 1}. - Writing the rows one by one. Three typed
<li>show correctly, but don't follow the data. The first exercise checks for an<liinside<For each=...>, and the second checks that each<li>'s content starts with{. - Using
.map(). It works, but makes every row again whenever the array changes, and the first exercise gradesFor. - Forgetting the wrapper. The
<li>rows go inside one<ul>, because what's checked isul li:first-childandul li:last-child.
TL;DR: <For each={fruits()}>{(item, i) => <li>{item}</li>}</For> makes one row per item: the item is a value, the position a getter i(), and a row is recognised by its object, so when the order changes the element moves along with its content (and a new object means a new row). <Index each={numbers()}>{(num, i) => <li>{i + 1}. {num()}</li>}</Index> is the reverse: the item is a getter num(), the position a number, and a row is tied to its position, so only the text changes. Use For for lists of objects that get sorted, added to, removed from; Index for lists whose positions stay but whose contents change.
⚡ For: one row per item
Write:
const [fruits] = createSignal(["Apple", "Mango", "Orange"])
<ul>
<For each={fruits()}>
{(item) => <li>{item}</li>}
</For>
</ul>
Meaning: each takes the array, and the content of <For> is a function called once per item. In For, item is the value itself, so it's written without parentheses. The position is a getter: i().
⚡ Index: one row per position
Write:
<Index each={numbers()}>
{(num, i) => <li>{i + 1}. {num()}</li>}
</Index>
Meaning: The roles are the reverse of For: the item is a getter, num(), and the position is a plain number, i. If the number at position 2 changes, Index only replaces the text in row 2.
⚡ For tracks items, Index tracks positions
Guess first: you type gift in the note of the row for The Rainbow Troops, then click Reverse order. In the Index version, where is that note?
Still in row 1, now next to Beauty Is a Wound. The rows never move, only their text is replaced. In the For version, the <li> moves along with its book.
Meaning: Use For for lists of objects that get sorted, added to, or removed from. Use Index for lists whose positions stay but whose contents change.