Sam’s recipe box forgot every recipe added since the page loaded, because each load runs main.js from the top, and main.js starts with the same five recipes. In this video, you saved the recipes in localStorage, first the wrong way, then as JSON text. Here are the key points:
localStorageis a small store of text that the browser keeps through both a reload and closing the browser. There’s one store per origin: the start of the address, up to the number after the colon, such ashttp://127.0.0.1:3000. ItssetItem()method saves a value under a name, called a key. Every page at that origin shares the store, so pick a key that names your app, such as"recipe-box".- The Application panel in DevTools shows what the browser keeps for a page. Open DevTools and pick the Application tab (if it’s hidden, it’s under the » menu). In the sidebar, under Storage, expand Local storage and pick your page’s origin, such as
http://127.0.0.1:3000. The table shortens long values, so select a row to see its whole value below the table, where DevTools lays out JSON as a tree you can expand. localStoragekeeps only strings, sosetItem()turns anything else into a string first. Handing it therecipesarray as it is saves the array’s items joined with commas, and an object turned into a string only says that it’s an object, so every recipe becomes the same text,[object Object]. The names, servings and minutes never reach the store.- The
getItem()method reads the value saved under a key. After that first attempt, it gives back the same[object Object]text, andJSON.parse()throws aSyntaxErroron it, because it was never JSON. The recipes can’t come back from it. - Save JSON text instead. Wrap the array in
JSON.stringify(), sosetItem()gets JSON text with every recipe’s fields. The old[object Object]text stays in the store until the next save, and eachsetItem()call replaces the whole value under its key. - Save after every change to the array: right after the form pushes a new recipe, and right after a delete.
- Watch out: a saved value comes back as a string. Even a number saved on its own, such as
6, comes back as the text'6', so adding1to it gives'61', not7, unless you convert the text to a number first. - Nothing reads the save yet. After a reload, the store still holds all six recipes, but the list shows five, because
main.jsbuilds the list from its own five recipes.
Inside setUpRecipeBox() in recipes/main.js, add saveRecipes(), and call it in the submit listener and in the Delete dialog’s close listener. The rest of the file is unchanged:
recipes/main.js
function saveRecipes() {
localStorage.setItem("recipe-box", JSON.stringify(recipes));
}
form.addEventListener("submit", (event) => {
// ...read the form, as before
recipes.push({ ... });
saveRecipes();
render();
// ...as before
});
dialog.addEventListener("close", () => {
// ...as before
const [recipe] = recipes.splice(index, 1);
saveRecipes();
render();
// ...as before
});
Add a recipe, such as Pea and mint soup, then open the Application panel and select the recipe-box row. It holds the six recipes as JSON text, with Pea and mint soup last.