Boss lesson · Unit 01
Lists and conditions
A product list end to end: map, keys that hold, conditions, and the stray 0.
What you practise
- Rendering with
map, keys and what goes wrong without stable ones, conditional rendering and the0 &&trap - A product list end to end
Try a question from this lesson
Pick one
A shop page gets its products as an array. What HTML does this produce?
const items = ['Tea', 'Mug'];
const out = (
<ul>
{items.map(t => <li key={t}>{t}</li>)}
</ul>
);Correct answer
<ul><li>Tea</li><li>Mug</li></ul>
Why
map returns an array of <li> elements, and React renders an array by rendering each item in order. key is a note for React, not an HTML attribute, so it never appears in the page.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Keys
When the array changes, React must work out which row is which: was one added, removed, moved? key is the answer you give it: a string or number that identifies this item among its siblings, usually the id from your data. A key must be stable (same item, same key, every render) and unique in its list.
Concept
Three ways to show or hide
An early return null renders nothing at all. cond ? a : b picks one of two. cond && a shows a or nothing, as long as cond is a real boolean. React renders nothing for false, null and undefined, which is what makes all three work.
type P = { user: string; unread: number };
function Bar({ user, unread }: P) {
if (!user) return null;
return (
<p>
{unread > 0 && <b>{unread}</b>}
{unread > 9 ? 'Busy' : 'Calm'}
</p>
);
}10 more exercises in the app
- 2× fill the blanks
- 4× multiple choice
- 1× true or false
- 2× type the answer
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.