useCartDetail.ts 807 B

123456789101112131415161718192021222324252627282930313233
  1. "use client";
  2. import { useAppDispatch, useAppSelector } from "@/store/hooks";
  3. import { fetchCartDetail } from "@/store/slices/cart-slice";
  4. import { useCallback } from "react";
  5. export function useCartDetail() {
  6. const dispatch = useAppDispatch();
  7. const cartDetail = useAppSelector((state) => state.cartDetail);
  8. const {cart, loading, error} = cartDetail;
  9. const getCartDetail = useCallback(async () => {
  10. dispatch(fetchCartDetail());
  11. }, [dispatch]);
  12. // // useCartDetail在组件中被调用时就会触发useEffect
  13. // // 初始化执行一次,获取购物车数据
  14. // useEffect(() => {
  15. // if (cart === null && !loading) {
  16. // getCartDetail();
  17. // }
  18. // }, [getCartDetail]);
  19. return {
  20. cartData: cart,
  21. getCartDetail,
  22. isLoading: loading,
  23. error,
  24. };
  25. }