CodingNic

Favorites and Conversion History

Save and Toggle Favorites

Favorites and Conversion History 18 min read

Save and Toggle Favorites

Save and Toggle Favorites

Open app.js and build Favorites around the existing from and to state. The finished project stores pair keys such as USD/EUR.

Start with a toggle function:

javascript
function toggleFavorite(pair) {
  let favorites = read("fx-favorites");
  favorites = favorites.includes(pair)
    ? favorites.filter(item => item !== pair)
    : [...favorites, pair];

  write("fx-favorites", favorites);
}

For the active pair, the key can come directly from the shared state:

javascript
const currentPair = () => `${from}/${to}`;

Test it

Click the Favorite control on Convert and inspect localStorage.

Checkpoint

The same pair should be added once and removed when the control is clicked again.