
The turtles are safe. Now let’s clean their beach! Costa Rica has beaches on two coasts: the Pacific Ocean and the Caribbean Sea. Sea turtles can mistake a floating plastic bag for a jellyfish, which many of them eat, so every piece of trash you pick up helps.
What you’ll learn
- How to keep a list in state
- How to remove an item from a list with
.filter() - How to show one button for each item in a list with
.map()
Open your file
In your StackBlitz tab, click Beach.jsx in the file list. It’s in the same components folder as Turtles.jsx.
In this activity, the trash buttons don’t show up until Step 3. Then everything works at once!
Step 1 β TODO 1: Put the trash list in state
trash is a list (programmers call it an array): items inside square brackets [ ], with commas between them. We want React to remember this list and update the screen when it changes. So, just like turtles in Activity 1, we put it in state. This time the state starts as a list instead of 0.
Find these lines:
const trash = [
"π§ Plastic bottle",
"πΎ Glass",
"π₯« Can",
"π Paper",
];
Change the first and last lines, so it looks like this:
const [trash, setTrash] = useState([
"π§ Plastic bottle",
"πΎ Glass",
"π₯« Can",
"π Paper",
]);
You don’t have to touch the items in the middle. Only two lines change:
- The first line:
const trash = [becomesconst [trash, setTrash] = useState([. - The last line:
];becomes]);.
What you should see: While only the first line is changed, the error box may show up. That’s normal! It goes away when you change the last line too. After that, nothing changes on the screen yet. That’s OK!
Step 2 β TODO 2: Pick up a piece of trash
pickUp will run when you click a piece of trash. The piece you clicked is called item. We use .filter() to make a new list that keeps every piece of trash that is not item. It’s like emptying your backpack and putting back everything except the banana peel. Then setTrash saves the new list.
Find these lines:
const pickUp = (item) => {
};
Type 1 new line in the middle, so it looks like this:
const pickUp = (item) => {
setTrash(trash.filter((t) => t !== item));
};
(t) => t !== item looks at each piece of trash, t, one at a time. !== means “is not the same as”.
What you should see: Nothing changes yet, because there are no buttons to click. That’s the next step!
Step 3 β TODO 3: Make a button for each piece of trash
.map() goes through a list one item at a time and turns each item into something to show. Here, each piece of trash becomes a button: 4 pieces of trash, 4 buttons. When the list gets shorter, there are fewer buttons.
Find these lines:
<div className="trash-buttons">
{/* TODO 3: Make one button for each piece of trash. */}
</div>
Replace the TODO 3 comment with this code, so it looks like this:
<div className="trash-buttons">
{trash.map((item) => (
<button key={item} onClick={() => pickUp(item)}>
Pick up {item}
</button>
))}
</div>
key={item}gives each button its own name, so React can keep track of them.onClick={() => pickUp(item)}means “when this button is clicked, pick up this item”. In Activity 1 the button saidonClick={saveTurtle}. Here we also have to tellpickUpwhich item, so we wrap it in a little function,() =>, that runs only when you click. Don’t forget it!- Look at the end:
))}. The first)closes the button part, the second closes.map(, and}closes the{at the start.
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.
What you should see: Four buttons appear: Pick up π§ Plastic bottle, Pick up πΎ Glass, Pick up π₯« Can and Pick up π Paper. Click one and it disappears! Pick up all four and you see The beach is clean!
That message comes from a line that was already in the file:
{trash.length === 0 && <p className="final-message">The beach is clean!</p>}
trash.length is how many items are in the list. && means “only if the part before me is true, show the part after me”. So the message shows only when the list is empty.
Checkpoint

Test your beach card:
- Do you see 4 buttons?
- Click one. Does only that button disappear?
- Click the rest. Do you see The beach is clean!?
Your turn
Add your own piece of trash to the list in TODO 1. Make a new line after "π Paper", and type something like "π Old shoe", with quotes around it and a comma at the end. Make it different from the other items. A new button appears!
Emoji are optional. To add one, press Windows key + . on Windows, or Ctrl + Cmd + Space on a Mac.
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
Beach.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";
export default function Beach() {
const [trash, setTrash] = useState([
"π§ Plastic bottle",
"πΎ Glass",
"π₯« Can",
"π Paper",
]);
const pickUp = (item) => {
setTrash(trash.filter((t) => t !== item));
};
return (
<div className="beach">
<h2>ποΈ Beach Cleanup</h2>
<p className="instructions">
Click the trash to pick it up and protect sea life.
</p>
<div className="trash-buttons">
{trash.map((item) => (
<button key={item} onClick={() => pickUp(item)}>
Pick up {item}
</button>
))}
</div>
{trash.length === 0 && <p className="final-message">The beach is clean!</p>}
</div>
);
}
Not working?
- No buttons appear. Check that your
.mapcode is between<div className="trash-buttons">and</div>. Also check that right after(item) =>you typed a round bracket(, not a curly one{. - The whole app disappeared. Check that your
.mapcode starts with{and ends with))}. - All the trash vanished right away, and it says “The beach is clean!” You probably wrote
onClick={pickUp(item)}, like the turtle button. Add() =>so it saysonClick={() => pickUp(item)}. Without it, React runspickUpfor every item as soon as it shows the buttons, instead of waiting for a click. - The buttons don’t go away when you click them. Check that your TODO 2 line is inside
pickUp, and thatsetTrashis spelled the same as in TODO 1, with a capital T. - The error box says
Unexpected tokenor'return' outside of function. You have a missing or an extra bracket. TODO 1 must end with]);and TODO 3 must end with))}. Compare your file with the complete file above. - You see
))}written on your card. The editor added))}for you and you typed it again. Delete one of them.