Lesson 2 · Unit 01
Props
Components are functions: props go in, elements come out, and data flows down.
What you practise
- Components take props and return elements, composition and
children, typed props, data flows down
Try a question from this lesson
Pick one
A component is a function that returns JSX. This page uses one twice. What HTML comes out?
type Props = { name: string };
function Hello({ name }: Props) {
return <p>Hi {name}</p>;
}
const out = (
<div>
<Hello name="Ada" />
<Hello name="Bo" />
</div>
);Correct answer
<div><p>Hi Ada</p><p>Hi Bo</p></div>
Why
React calls Hello once per tag and passes the attributes as one object: { name: "Ada" }, then { name: "Bo" }. Each call returns its own <p>. The <Hello> tag never reaches the HTML; only what the function returns does.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Props
A component is a function whose name starts with a capital letter. It takes one argument, the props object, built from the attributes on its tag. Destructure it in the parameter list and give it a type: then a missing or misspelled prop is a compile error, not a blank spot on the page.
type Props = {
title: string;
mins: number;
};
function Task({ title, mins }: Props) {
return <li>{title} ({mins} min)</li>;
}
const t = <Task title="Ship" mins={30} />;Concept
Composition
Because children can be any JSX, a component can be a frame that does not know what it frames: a card, a dialog, a page layout. You build screens by nesting small components, the way HTML nests elements. Data moves one way only: a parent passes props down, and a child never reaches up to change them.
Layout
└─ Card title="Inbox"
└─ MessageList messages={...}
└─ Message (one per message)10 more exercises in the app
- 5× multiple choice
- 2× fill the blanks
- 1× true or false
- 1× order the lines
- 1× type the answer
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.