Lists

5 min·Assumes: state, events, computed

@for repeats an element once for every item in a list.

import { state } from '@markless/core';

export default function Weekend() @{
	let plans = state(['gym', 'laundry', 'taxes']);

	<section>
		<ul>
			@for (const plan of plans; key plan) {
				<li>{plan}</li>
			}
		</ul>
		<button onClick={() => plans.pop()}>
			Feel a bit tired
		</button>
	</section>
}

@for repeats the <li> once for each plan in plans, and key tells the items apart. The button removes the last plan from plans, and its <li> leaves the page.

The key says which item is which

Give each loop item a key, and whatever the reader did to it (a like, a tick, half-typed text) stays with it when the list changes order.

Drag any man to another spot, and see who each slip points at.

It was number 3.

key i

It was Mr Moss.

key suspect.id
An ink drawing of a police lineup. The numbers 1 to 5 are painted on the wall, and five made-up men stand in front of it, each holding a card with his own name. Under the lineup are the witness's two slips of paper. The left one says "It was number 3." and its arrow points at spot 3, labelled key i. The right one says "It was Mr Moss." and its arrow points at Mr Moss, labelled key suspect.id. At the start Mr Moss stands on spot 3, so both arrows point at him. Drag a man onto another spot, or focus him and press the left or right arrow key, and the two men swap places. The name slip's arrow goes with Mr Moss. The number slip's arrow stays on spot 3 and points at whoever stands there now. Escape puts everyone back.

key suspect.id tells the items apart by who the man is, so the slip follows him. key i tells them apart by where they stand, so the slip stays on spot 3 and accuses whoever is there now.

Try it on a timeline

Key items by their position and a like lands on the wrong post as soon as a new one arrives. Like the engagement, then show the new post.

PlaygroundRestored your edit
Sam & Alex@samandalex · 2m

We got engaged!!

Priya@threecoffees · 1h

Monday. Coffee number three.

Try this
  1. To do: Like “We got engaged!!”, then press Show 1 post. Look at which post your like is on now.
  2. To do: Change index i; key i to key post.id and run it again. Like the engagement, then press Show 1 post: your like stays on the engagement.

Sort and filter without changing the list

Show a list sorted or filtered without changing it, and the copy keeps itself up to date: put the sorted list in a computed value and loop over that.

Tap a snail to cheer it on.

PlaygroundRestored your edit
  1. 1stPebble0
  2. 2ndDoris0
  3. 3rdSlick0
  4. 4thNibbles0

Tap a snail to cheer it on.
Try this
  1. To do: Cheer Pebble, the pink snail, all the way to the lettuce.
  2. To do: Put the last snail on top: in race.tsrx, change b.distance - a.distance to a.distance - b.distance, then race again.
  3. To do: Show only the podium: add .slice(0, 3) after the sort, then race. Watch a snail drop off the board and come back.

Split one list into several

Change a list once, and every place that shows part of it updates: make each place a computed filter() of the one list. @empty is what a place shows when it has no items.

Drag a shape into a hole.

PlaygroundRestored your edit

On the tray: 6

Try this
  1. To do: Drag the square into the square hole. Then try the star in the square hole too.
  2. To do: Push all six through the square hole. The other three holes still show their cobweb: that is @empty.
  3. To do: Fix the toy: in rules.ts, make fits return hole === shape.kind;, then try the star on the square hole again.

No mouse? Focus a shape, press 1 to 4 to choose a hole, then Enter.

How do I show each item's number?

Add index to the loop. It counts from 0 and follows the order on the page:

@for (const runner of board; index place; key runner.id) {
	<li>{place + 1}. {runner.name}</li>
}
What makes a good key?

Something that belongs to the item and never changes, like an id. Write it as the item or one of its fields (key item, key item.id); anything calculated, like key String(item.id), stops the build. The position works only for a list that never changes order, and the build warns you when you use it.

Coming from React?

The key does the same job as key in React. Here it is part of @for itself, and leaving it out stops the build.

Button sounds are from Josh Comeau's Button Sounds library.