"use client"; import { useAppDispatch, useAppSelector } from "@/store/hooks"; import { fetchCartDetail } from "@/store/slices/cart-slice"; import { useCallback } from "react"; export function useCartDetail() { const dispatch = useAppDispatch(); const cartDetail = useAppSelector((state) => state.cartDetail); const {cart, loading, error} = cartDetail; const getCartDetail = useCallback(async () => { dispatch(fetchCartDetail()); }, [dispatch]); // // useCartDetail在组件中被调用时就会触发useEffect // // 初始化执行一次,获取购物车数据 // useEffect(() => { // if (cart === null && !loading) { // getCartDetail(); // } // }, [getCartDetail]); return { cartData: cart, getCartDetail, isLoading: loading, error, }; }