Skip to main content
Frontend tutorial

4.6. Action for deleting

If we can add counters, we should also be able to remove them. The action keeps every counter except the one we pass:

export class Store {
// ...
removeCounter = (counter: CounterStore) => {
this.counters = this.counters.filter(c => c !== counter);
};
}

Counter can reach the store directly, so the X button calls the action itself. No callback props needed:

return <div className="counter">
<output>{value}</output>
<button className="remove" aria-label="Remove"
onClick={() => store.removeCounter(counter)}>X</button>
<button onClick={() => add(-100)}>-100</button>
Need a hint?
  • filter returns a new array. Assign it back to this.counters.

Add counters until the + disappears, then remove one. The + comes back on its own, because canAddMoreCounters follows the list.

Work locally

You need Node.js 24, pnpm and Git (how to set them up). Clone this step’s starting code:

git clone --depth 1 \
--branch lesson-04-06-delete-start-v6 \
https://github.com/adam-zielonka/frontend-tutorial.git \
04-06-delete
cd 04-06-delete
pnpm install --frozen-lockfile
pnpm dev

To keep your own commits: git switch -c my-work.

Browse the source on GitHub (opens in a new tab)

Bring your browser edits

Choose Download my files in the ⋯ menu. It saves the lesson’s files as JSON. Stop the local server with Ctrl+C, copy the download into the project folder and run:

node import-draft.mjs ./04-06-delete-my-files.json
pnpm dev

The import replaces the project’s exercise files, so commit or back up local edits first.

Loading this step’s code…
Starting the live preview…