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.