import { useAddToCartMutation } from “./api/cart”; export default function Product({…
- User
import { useAddToCartMutation } from “./api/cart”;
export default function Product({ product }) { const [addToCart, { isLoading }] = useAddToCartMutation(); const handleClick = () => { addToCart(product).unwrap() .then((resp) => { // Обработка успешного ответа }) .catch((error) => { // Обработка ошибки }); };
return (
// Обновление состояния после добавления в корзину export const useAddItemToCart = () => { const [addToCart] = useAddToCartMutation(); const [addItemToCart, { isLoading }] = useDispatch( cartSlice.actions.addItem );
return [ (item) => addToCart(item).unwrap().then((resp) => { // Добавление элемента в store после успешного запроса addItemToCart(resp); }), isLoading, ]; }; Обьедени их в 1 файл
- Assistant
import { useAddToCartMutation } from "./api/cart"; import { useDispatch } from "react-redux"; import { cartSlice } from "./redux/cartSlice";
export default function Product({ product }) { const [addToCart, { isLoading }] = useAddToCartMutation(); const handleClick = () => { addToCart(product) .unwrap() .then((resp) => { // Обработка успешного ответа }) .catch((error) => { // Обработка ошибки }); };
return ( {product.title} {product.description} Add to Cart ); }
export const useAddItemToCart = () => { const [addToCart] = useAddToCartMutation(); const [addItemToCart, { isLoading }] = useDispatch(cartSlice.actions.addItem);
return [ (item) => addToCart(item) .unwrap() .then((resp) => { addItemToCart(resp); }), isLoading, ]; };
- User
import {useGetProductsQuery} from "../../api/productsApi"; import plus from '../../assets/icon/plus.svg' import {useDispatch} from "react-redux"; import {useAddCartItemMutation} from "../../api/cartApi";
const HomePage = () => { const { data, error, isLoading, isError } = useGetProductsQuery(); const [selectedCategory, setSelectedCategory] = useState('all') const dispatch = useDispatch(); // Инициализируем хук useDispatch const [addToCart] = useAddCartItemMutation()
ERROR api_cartApi__WEBPACK_IMPORTED_MODULE_10_.useAddCartItemMutation is not a function HomePage@http://localhost:3000/main.420f5c354b1f84d5ca72.hot-update.js:123:92 renderWithHooks@http://localhost:3000/static/js/bundle.js:34419:31 mountIndeterminateComponent@http://localhost:3000/static/js/bundle.js:37705:17 beginWork@http://localhost:3000/static/js/bundle.js:39001:20 callCallback@http://localhost:3000/static/js/bundle.js:24011:18 invokeGuardedCallbackDev@ invokeGuardedCallback@ beginWork$1@ performUnitOfWork@
- Assistant
Looks like an issue with the import, please check if you are importing the
useAddCartItemMutationfrom the correct path. Make sure that the import path is correct and matches with the correct file path. - User
- Assistant
It looks like the import for the
addCartMutationis incorrect in the HomePage.js file. Please check the import statement to ensure that it is correctly importing from../../api/cartApias follows:import { useAddCartItemMutation } from "../../api/cartApi";Also, please make sure that
useAddCartItemMutationis exported from the api/cartApi.js file.