I'm trying to create a custom hooks in order to store Cart data.
The cart object:
{ uid: quantity, uid2: quantity2, ...}
The Hook I did:
import { useState } from "react";
// Return a new object without empty values
const clean = (obj) =>
Object.keys(obj).reduce((acc, key) => {
if (obj[key]) {
acc[key] = obj[key];
}
return acc;
}, {});
export const useCart = () => {
// Cart is [uid]=quantity
const [cart, setCart] = useState({});
const updateCart = ({ uid = 0, quantity = 1 }) => {
const newCart = clean({
...cart,
[uid]: quantity > 0 ? quantity : null
});
setCart(newCart);
console.log(" New Cart : ", JSON.stringify(newCart));
};
// Do are they reactive ? Or do we need to useState them
const count = Object.values(cart).reduce((acc, value) => acc + value, 0);
// Return all
return { cart, updateCart, setCart, count };
};
The issue I have
The cart works fine for a single Item but when I update the DOM and re-ask for cart data, they're it's back to initial value.
I'm pretty sure it's because the updateCart() is initialized with initial value who isn't updated... but I don't know how to do it ?
I also tried to use directly setCart() on components instead of updateCart() but nothing better.
I did an example to test it without my project: https://codesandbox.io/s/reverent-tess-8pymji
Thanks.