
Last stop: the rainforest! Costa Rica has two kinds of sloths, two-toed and three-toed. They move so slowly that tiny plant-like living things called algae grow on their fur, which helps them hide in the trees. Four sloths need your help!
What you’ll learn
- How to use one small component many times
- How to send information to a component with props
- How to make one component for each item in a list with
.map()
Open your file
Click Sloths.jsx in the file list (src → components).
This file has two components:
Sloth, at the top, is small. It shows one sloth card.Sloths, further down, is big. It’s the whole rescue area, and it usesSlothcards inside it.
Sloth is like a cookie cutter, and each <Sloth /> is a cookie. One cutter, lots of cookies!
Step 1 — TODO 1: Show the sloth’s name
When the big component writes <Sloth name="Mau" ... />, it hands the small one some information. This information is called props. The small component catches its props in its first line: function Sloth({ name, saved, onRescue }). But right now the card ignores name and always says “A sloth”.
Find this line in the small Sloth component, near the top:
<h3>🦥 A sloth</h3>
Change it to:
<h3>🦥 {name}</h3>
Only change A sloth to {name}, and keep the 🦥. The curly braces { } mean “show the value of name”. Without them, you would just see the word “name”.
What you should see: The card now says 🦥 Mau.
Step 2 — TODO 2: One card for each sloth
In the big Sloths component there’s a list of four names: const sloths = ["Mau", "Rick", "Luna", "Toby"];. But only Mau gets a card, because the <Sloth ... /> line is written just for Mau. Let’s use .map() to make one card for each name, like the trash buttons in Activity 2.
Find this line near the bottom, under the TODO 2 comment:
<Sloth name="Mau" saved={rescued.includes("Mau")} onRescue={rescue} />
Delete it and type this in its place:
{sloths.map((name) => (
<Sloth
key={name}
name={name}
saved={rescued.includes(name)}
onRescue={rescue}
/>
))}
Each card gets a key and three props:
key={name}: a name React uses to keep track of each card.name={name}: the sloth’s name. You show it on the card in TODO 1.saved={rescued.includes(name)}:trueif this sloth is on the rescued list,falseif not.onRescue={rescue}: the function to run when you click Rescue. There’s no()afterrescue, because we’re handing the function over, not running it yet.
When you type ( or {, the editor sometimes adds the closing ) or } for you. Check that your code matches the box above, with no extra ))} left over.
Already in the file for you: rescued is a list in state that starts empty, useState([]). The rescue function adds a sloth’s name to that list:
const [rescued, setRescued] = useState([]);
const rescue = (name) => {
if (!rescued.includes(name)) {
setRescued([...rescued, name]);
}
};
!rescued.includes(name) means “if this name is not on the list yet”. [...rescued, name] means “everything already on the list, plus this new name”.
What you should see: Four cards: Mau, Rick, Luna and Toby. Click Rescue on a card. It turns green and says ✅ Safe! Rescue all four and you see You rescued all the sloths!
Checkpoint

Test your sloth cards:
- Do you see four cards with four different names?
- Click Rescue on two of them. Do they turn green and say ✅ Safe!?
- Rescue the rest. Do you see You rescued all the sloths!?
Your turn
Add your own sloth to the sloths list, like "Pura Vida" (that’s how people in Costa Rica say hello, thanks and “all good”!). Type it after "Toby", before the ]. Put quotes around the name and a comma before it, and make it different from the other names. With Pura Vida, the line looks like this:
const sloths = ["Mau", "Rick", "Luna", "Toby", "Pura Vida"];
A new card appears, and now you have to rescue 5 sloths to finish.
Stuck?
Compare your code with the finished file. Click Show the complete file to open it.
To catch up, you can replace your whole file with it:
- Click just before
importat the top of the box, then hold Shift and click after the last}at the bottom. Copy it (Ctrl + C, or Cmd + C on a Mac). - In StackBlitz, click inside
Sloths.jsxand press Ctrl + A (Cmd + A on a Mac) to select everything. - Paste (Ctrl + V, or Cmd + V on a Mac).
▶ Show the complete file
import { useState } from "react";
// A SMALL component: one sloth card.
// It gets its information from props: name, saved and onRescue.
function Sloth({ name, saved, onRescue }) {
return (
<div className={saved ? "sloth-card saved" : "sloth-card"}>
<h3>🦥 {name}</h3>
<p>{saved ? "✅ Safe!" : "Needs help"}</p>
{!saved && <button onClick={() => onRescue(name)}>Rescue</button>}
</div>
);
}
// A BIG component: the whole rescue area.
export default function Sloths() {
const sloths = ["Mau", "Rick", "Luna", "Toby"];
const [rescued, setRescued] = useState([]);
const rescue = (name) => {
if (!rescued.includes(name)) {
setRescued([...rescued, name]);
}
};
return (
<div className="sloths">
<h2>🦥 Sloth Rescue</h2>
<p className="instructions">Click a sloth to rescue it from the forest.</p>
<div className="sloth-list">
{sloths.map((name) => (
<Sloth
key={name}
name={name}
saved={rescued.includes(name)}
onRescue={rescue}
/>
))}
</div>
{rescued.length === sloths.length && (
<h3 className="final-message">You rescued all the sloths!</h3>
)}
</div>
);
}
Not working?
- The card still says “A sloth”. TODO 1 goes in the small
Slothcomponent at the top of the file, not in the big one. - Every card says “name”. You forgot the curly braces. It should be
{name}. - You see Mau twice (5 cards). You added the
.mapcode but didn’t delete the old<Sloth name="Mau" ... />line. Delete it. - No sloth cards at all. Component names start with a capital letter:
<Sloth, not<sloth. Also check that right after(name) =>you typed a round bracket(, not a curly one{. - The whole app disappeared. Check that your
.mapcode starts with{and ends with))}. - Clicking Rescue does nothing. Check that it says
onRescue={rescue}, with no()afterrescue. - You see
))}written on your card. The editor added))}for you and you typed it again. Delete one of them. - The error box says
Unexpected tokenor'return' outside of function. You have a missing or an extra bracket. Compare your file with the complete file above.