Components

4 min·Assumes: state, events, computed, conditionals

A
is a function that returns elements and state. Write it once, then use it in as many places as you like.
function Avatar({ name }: { name: string }) @{
	<img
		src={`/faces/${name}.jpg`}
		alt={`${name}'s profile picture`}
	/>
}

export default function Post() @{
	<article>
		<h1>Why our standup takes an hour</h1>
		<p>Written by</p>
		<Avatar name="Priya" />
		<Avatar name="Tom" />
		<Avatar name="Ines" />
	</article>
}

Avatar is the component here. Use it like a tag, <Avatar />, once for each author. name="Tom" passes it a value, called a prop.

Every copy of a component has its own state

State inside a component belongs to that one copy. Use <Toy /> three times and each toy counts its own turns.

Turn the robot's pink key clockwise, then let go. Only the robot walks.

Toy is in its own file, toy.tsrx. Import it wherever you want to use it.

PlaygroundRestored your edit

Turns: 0

Turns: 0

Turns: 0

Try this
  1. To do: Turn the robot's key clockwise three full turns, then let go. The mouse and the frog stay put.
  2. To do: In nursery.tsrx, add a second <Toy kind="frog" /> below the first. The new frog has its own key and its own turns.

Share one piece of state between components

Pass state to a component like any other value. Change it once, and every spot that shows it changes. No component runs again.

Tap Biscuit's photo under Switch account.

PlaygroundRestored your edit
Robin's photo
Robin's photo

Robin's photoRobin Great view!

Try this
  1. To do: Tap Biscuit's photo under Switch account. The top bar, the profile card and the comment all show the cat.
  2. To do: Tag yourself in the lake photo: in feed.tsrx, add <Avatar name={me} /> on its own line just above <p class="comment">. The new one switches with the other three.

Pass a function to a component, and run it when needed

A function is passed in like any other value. Inside the component, call it at the moment you choose, such as when a button is pressed.

Light the gold rocket.

PlaygroundRestored your edit

Ooh!

Oohs: 0

Try this
  1. To do: Light the gold rocket. When it bursts, the crowd counts it.
  2. To do: Light the red one and the green one. Every rocket calls the same function, so they all add to one count.
  3. To do: In fireworks.tsrx, copy the green rocket's line and change green to silver. The new rocket counts too.

Pass elements to a component as children

Whatever you write between a component's tags arrives inside it as children. The component shows them wherever it puts {children}.

Swipe the screen sideways.

PlaygroundRestored your edit

9:41

Messages

Lunch at one?

On my way!

I'll get a table

9:41

Photos

9:41

Music

Morning Sun

Night Bus

Paper Kites

Try this
  1. To do: Swipe to the next screen. The status bar, title and home bar stay; what is between the tags of that <Screen> changes.
  2. To do: In app.tsrx, write a new screen after Music: <Screen title="Notes"><p>Buy milk</p></Screen>. Then swipe to it.
Can a prop have a default value?

Yes. Write it in the parameter list:

function Stars({ size = 3 }: { size?: number }) @{
	<p>{size} stars</p>
}

size is 3 whenever the prop is left out.

Can a function you pass in receive more than one value?

Yes, as one object. The component calls your function with a single value, so put the values in an object:

onBang({ color, height });

Your function takes the object apart:

<Rocket onBang={({ color, height }) => …} />

Calling it with two values, onBang(color, height), stops the build.

Coming from React?

Four things differ:

  • A component runs once, when it is first shown. It does not run again when state changes, so there is no useMemo or useCallback.
  • Props still stay current. A spot that shows a prop changes when the parent's state changes.
  • A value calculated from a prop goes in computed(). A plain const would be calculated once and never again.
  • children can only be placed, with {children}. They cannot be counted or mapped.

One thing is the same: a component cannot change its props. To change something the parent owns, call a function the parent passed.

Next, keep a value after a reload.

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