Skip to content

Instantly share code, notes, and snippets.

@becauro
Last active August 19, 2021 19:18
Show Gist options
  • Save becauro/eb6d306164b61a3067c39ea442e2074a to your computer and use it in GitHub Desktop.
Save becauro/eb6d306164b61a3067c39ea442e2074a to your computer and use it in GitHub Desktop.
How to set a Favorite content to LocalStorage. In this case, the content come from recipeDetail Props
addToLocalStorage() {
const { id, type, recipeDetail: { strArea, strCategory,
strAlcoholic, strDrink, strMeal, strMealThumb, strDrinkThumb } } = this.props;
const favoriteRecipes = JSON.parse(getItem('favoriteRecipes')) || [];
const newFavoriteRecipe = [
...favoriteRecipes,
{
id,
type: type.replace('s', ''),
area: strArea || '',
category: strCategory || '',
alcoholicOrNot: strAlcoholic || '',
name: strMeal || strDrink,
image: strDrinkThumb || strMealThumb,
},
];
setItem('favoriteRecipes', JSON.stringify(newFavoriteRecipe));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment