Untitled
unknown
javascript
3 years ago
687 B
6
Indexable
const ShopContext = React.createContext();
export const ShopProvider = ({ children }) => {
const [cart, setCart] = useState({});
//...other methods
const getCart = async () => {
const cart = await postToShopify(getCartQuery(), {
cartId: localStorage.cart_id.toString(),
});
setCart(cart.data.cart);
};
useEffect(() => {
if (localStorage.cart_id) {
getCart();
console.log(cart);
}
}, []);
return (
<ShopContext.Provider
value={{
cart,
}}
>
{children}
</ShopContext.Provider>
);
};
const ShopConsumer = ShopContext.Consumer;
export { ShopConsumer, ShopContext };
export default ShopProvider;Editor is loading...