diff --git a/lib/commerce/cart/index.tsx b/lib/commerce/cart/index.tsx index 76ef50c46..09bb0c419 100644 --- a/lib/commerce/cart/index.tsx +++ b/lib/commerce/cart/index.tsx @@ -1,4 +1,4 @@ -import { createContext, useContext, FC, useCallback } from 'react' +import { createContext, useContext, FC, useCallback, useMemo } from 'react' import useSWR, { responseInterface } from 'swr' import Cookies from 'js-cookie' import { useCommerce } from '..' @@ -22,7 +22,9 @@ const CartProvider: FC = ({ children, query, url }) => { }) return ( - // Avoid destructuring in `response` so we don't trigger the getters early + // Avoid destructuring the `response` obj from SWR so we don't trigger the getters + // early, also the result of useSWR is memoized and it's better to keep it that way + // so we don't re-render every consumer {children} diff --git a/lib/commerce/index.tsx b/lib/commerce/index.tsx index 8de21fdf3..bc125f210 100644 --- a/lib/commerce/index.tsx +++ b/lib/commerce/index.tsx @@ -1,11 +1,4 @@ -import { - createContext, - ReactNode, - useCallback, - useContext, - useMemo, -} from 'react' -import useSWR from 'swr' +import { createContext, ReactNode, useContext, useMemo } from 'react' const Commerce = createContext(null)