Components and props

2 min·Assumes: state, events

A
is a function that returns
, and props are the values you hand it.
function NameTag({ name }: { name: string }) @{
	<p>Hello, {name}</p>
}

export default function Welcome() @{
	<NameTag name="Ada" />
}

NameTag is a component: you place it with <NameTag />. name="Ada" is a prop, and { name } receives it, so the page shows Hello, Ada.

A prop is a live read

A child runs once, and its props keep reading the parent's current value.

  • The parent changes its state.
  • Every child that reads it shows the new value.
  • No child is placed again.

Drag a few Avatar stickers onto the page, then turn its name selector.

<Avatar>AdaGraceLinusnameAdaAdaAdaAdaname={name}
A pen-and-ink sticker sheet labelled <Avatar> lies beside a page with a name selector: Ada, Grace or Linus. Press a sticker, or drag it onto the page, and it becomes an Avatar badge that shows the page's name. Place several. Turn the selector, by dragging it, pressing it or with the arrow keys, and every placed sticker shows the new name at once, without being placed again. Try to write on a placed sticker, by pressing it or typing on it, and the ink beads off: only the page can change name. Drag a sticker off the page, or press Delete, to peel it back.

Ram Altman blows up the hype. Bubble is the child: it picks its colour once, when it runs, and reads size from the parent every time the parent pumps.

PlaygroundRestored your edit

AI is going to be big.

Try this
  1. To do: Press Pump four times. The bubble grows with size until it pops, and its colour never changes, because Bubble ran once. Pump again: the next bubble has the same colour.
  2. To do: Hand the bubble a fixed size instead: change it to <Bubble size={3} />, and press Pump. Ram keeps talking, but the bubble stops growing, because it no longer reads hype.

A prop worked out only from values outside the component, such as a module constant or what an imported function returns, keeps the value it rendered with. If that value can change later, keep it in state() so the child follows it.

A callback talks back

A child can't change the parent's state. The parent hands it a callback: a function the child calls.

A callback takes one value at most, so send more as one object.

Mark Zuckerbot comes with a remote. Each Key is a child; pressing one calls onPress, and the parent decides what Mark does.

PlaygroundRestored your edit

Awaiting a human instruction.

Try this
  1. To do: Press wave on the remote. Key calls onPress, and Mark waves.
  2. To do: Add a third key, <Key act="sip" onPress={press} />, and press it.
  3. To do: Try breaking it: give the wave key a callback that takes two values, onPress={(act, loud) => (mode = act)}. The build stops, because a callback takes one.

Children go where you put them

Elements between a component's tags arrive as its children prop, typed Children. The component decides where, and when, they show.

Two portraits hang under a sheet. Frame puts its children in the window, and only once you unveil it.

PlaygroundRestored your edit
Try this
  1. To do: Press Unveil on both frames. The sheet comes off, and each frame shows its children where {children} sits.
  2. To do: Add a line to Frodough's frame, under his name: <p>To-dos done: 0</p>.

When a handler needs the real <input>, reach a real element.