Sharky floating above a beach while three kids pick up trash and put it in green recycling bags

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

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:

  1. The first line: const trash = [ becomes const [trash, setTrash] = useState([.
  2. 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>

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

The beach card with four green buttons: Pick up Plastic bottle, Pick up Glass, Pick up Can and Pick up Paper

Test your beach card:

  1. Do you see 4 buttons?
  2. Click one. Does only that button disappear?
  3. 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:

  1. Click just before import at 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).
  2. In StackBlitz, click inside Beach.jsx and press Ctrl + A (Cmd + A on a Mac) to select everything.
  3. 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?