Medium
In a React e-commerce application, you need to implement a shopping cart hook that persists across page refreshes. What's wrong with this custom hook implementation?
1function useShoppingCart() {2 const [cart, setCart] = useState([]);34 useEffect(() => {5 const savedCart = localStorage.getItem('cart');6 if (savedCart) {7 setCart(JSON.parse(savedCart));8 }9 }, []);1011 const addToCart = (product) => {12 const updatedCart = [...cart, product];13 setCart(updatedCart);14 localStorage.setItem('cart', JSON.stringify(cart));15 };1617 const removeFromCart = (productId) => {18 const updatedCart = cart.filter(item => item.id !== productId);19 setCart(updatedCart);20 localStorage.setItem('cart', JSON.stringify(cart));21 };2223 return { cart, addToCart, removeFromCart };24}