Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Buttons do not work Uncaught TyperError: onUpdateCartQty is not a function

I’m building an e-commerce application and for some reason my buttons to update the cart aren’t working. I’m using props and when I go to the console log it says that onUpdateCartQty is not a function. I know props are a bit tricky.

Can someone point out what I did wrong?

CartItem.jsx

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import React from 'react';
import { Typography, Button, Card, CardActions, CardContent, CardMedia } from '@material-ui/core';

import useStyles from './styles';

const CartItem = ({ item, onUpdateCartQty, onRemoveFromCart }) => {
const classes = useStyles();

return (
  //product.image.url?
<Card>
    <CardMedia image={item.image.url} alt={item.name} className={classes.media} />
    <CardContent className={classes.cardContent}>
        <Typography variant="h4">{item.name}</Typography>
        <Typography variant="h5">{item.line_total.formatted_with_symbol}</Typography>

    </CardContent>
    <CardActions className={classes.cardActions}>
        <div className={classes.buttons}>
                <Button type="button" size= "small" onClick={() =>  onUpdateCartQty(item.id, item.quantity - 1)}>-</Button>
                <Typography>{item.quantity}</Typography>
                <Button type="button" size= "small" onClick={() =>  onUpdateCartQty(item.id, item.quantity + 1)}>+</Button>
        </div>
        <Button variant="contained" type= "button" color="secondary" onClick={() => onRemoveFromCart(item.id) }>Remove</Button>

    </CardActions >
</Card>

)
}

export default CartItem

App.js

import React, { useState, useEffect } from "react";
import { commerce } from './lib/commerce';
import { Products, Navbar, Cart } from './components';
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";




const App = () => {
const [products, setProducts] = useState([]);
const [cart, setCart] = useState({});

const fetchProducts = async () => {
  const { data } = await commerce.products.list();

  setProducts(data);

}

const fetchCart = async () => {
  setCart(await commerce.cart.retrieve())
}

const handleAddToCart = async (productId, quantity) => {
  const { cart } = await commerce.cart.add(productId, quantity);

  setCart(cart);
}

const handleUpdateCartQty = async (productId, quantity) => {
const { cart } = await commerce.cart.update(productId, { quantity});

setCart(cart);

}

const handleEmptyCart = async () => {
  const { cart } = await commerce.cart.empty();

  setCart(cart);
}

const handleRemoveFromCart = async (productId) => {
  const { cart } = await commerce.cart.remove(productId);

  setCart(cart);
}

  useEffect(() =>{
  fetchProducts();
  fetchCart();
}, []);

console.log(cart);


 return (
 <Router>
 <div>
  <Navbar totalItems={cart.total_items} />
  <Routes>
    <Route path= "/" element={<Products products = {products} onAddToCart= 
    {handleAddToCart}/>}/>
    {/* <Products  products={products} onAddToCart={handleAddToCart} /> */}
    {/* </Route> */}

    <Route path= "/cart" element= {<Cart cart={cart}/>}
    handleUpdateCartQty ={handleUpdateCartQty}
    handleRemoveFromCart ={handleRemoveFromCart}
    handleEmptyCart ={handleEmptyCart}
    />
    {/* <Cart cart={cart} /> */}
    {/* </Route> */}
      
      
  </Routes>
</div>
</Router>
);
};

export default App;

enter image description here

>Solution :

Currently, you are passing props to Route component not CartItem component.

 <Route path= "/cart" element= {<Cart cart={cart}/>}
handleUpdateCartQty ={handleUpdateCartQty}
handleRemoveFromCart ={handleRemoveFromCart}
handleEmptyCart ={handleEmptyCart}
/>

Try to send as

 <Route path= "/cart" element= {<Cart cart={cart}
handleUpdateCartQty ={handleUpdateCartQty}
handleRemoveFromCart ={handleRemoveFromCart}
handleEmptyCart ={handleEmptyCart}
/>} />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading