import { useState, useEffect } from 'react'; import AsyncStorage from…
- User
import { useState, useEffect } from 'react'; import AsyncStorage from '@react-native-async-storage/async-storage';
const FAVORITES_KEY = 'FAVORITE_GIFS';
export const useFavoriteGifs = () => { const [favorites, setFavorites] = useState([]);
useEffect(() => { const loadFavorites = async () => { try { const storedFavorites = await AsyncStorage.getItem(FAVORITES_KEY); if (storedFavorites) { setFavorites(JSON.parse(storedFavorites)); console.log('Loaded favorites from storage:', JSON.parse(storedFavorites)); } } catch (error) { console.error('Error loading favorites:', error); } }; loadFavorites(); }, []);
const saveFavorites = async (newFavorites) => { try { setFavorites(newFavorites); await AsyncStorage.setItem(FAVORITES_KEY, JSON.stringify(newFavorites)); console.log('Saved favorites to storage:', newFavorites); } catch (error) { console.error('Error saving favorites:', error); } };
const addFavorite = async (id) => { const newFavorites = [...new Set([...favorites, id])]; // Ensure no duplicates await saveFavorites(newFavorites); console.log('Added favorite:', id); };
const removeFavorite = async (id) => { try { const newFavorites = favorites.filter((favorite) => favorite !== id); await saveFavorites(newFavorites); console.log('Removed favorite:', id); } catch (error) { console.error('Error removing favorite:', error); } };
return { favorites, addFavorite, removeFavorite, }; };
The remove function is broken. Why? It removed the item but once it call for addItem, the old removed items appear again.
- Assistant
- User
the problem still exist.
- Assistant