Unturect typeError:AddItem不是OnClick的功能(itemcard.js:16:1)

发布于 2025-01-23 03:35:49 字数 4931 浏览 1 评论 0原文

这是我每次尝试在购物车中添加一些东西的错误。

Unturect typeError:AddItem不是OnClick(ItemCard.js:16:1)的功能。

我无法弄清楚我在做什么错。我是新手做出反应的新手,因此将不胜感激。

这是我的itemcard.js

import {useCart} from 'react-use-cart'

const ItemCard = (props) => {
const {addItem}=useCart();

 return (
   <div className='col-11 col-md-6 col-lg-3 mx-0 mb-4'>
   <div class="card p-0 overflow-hidden h-100 shadow" >
       <img src={props.img} class="card-img-top img-fluid" alt=""/>
       <div class="card-body text-center">
           <h5 class="card-title">{props.title}</h5>
           <h5 class="card-title">${props.price}</h5>
           <p class="card-text">{props.desc}</p>
           <buttton class="btn btn-success"
            onClick={()=>addItem(props.item)}
            >Add to Cart</buttton>
       </div>
   </div>
   </div>
 )
}

export default ItemCard```

这是我的购物车。

import React from 'react'
import { useCart } from 'react-use-cart'
import ItemCard from './ItemCard';

const Cart = () => {

const {
    isEmpty,
    totalUniqueItems,
    totalItems,
    items,
    cartTotal,
    updateItemQuantity,
    removeItem,
    emptyCart,
    }=useCart();
    if(isEmpty) return <h1 className='text-center'>Your Cart is Empty</h1>

  return (
      <section className='py-4 container'>
        <div className='row justify-content-center'>
            <div className='col-12'>
                <h5>Cart ({totalUniqueItems}) total Items:({totalItems})</h5>
                <table className='table table-light table-hover m-0'>
                    <tbody>
                    {items.map((item,index)=>{
                        return(
                            <tr key={index}>
                                <td>
                                    <img src={item.img} style={{height:'6rem'}}/>
                                </td>
                                <td>{item.title}</td>
                                <td>{item.price}</td>
                                <td>Quantity ({item.quantity})</td>
                                <td>
                                    <button className='btn btn-info ms-2'
                                     onClick={()=>updateItemQuantity(item.id, item.quantity - 1)}
                                    >-</button>
                                    <button className='btn btn-info ms-2'
                                    onClick={()=>updateItemQuantity(item.id, item.quantity + 1)}
                                    >+</button>
                                    <button className='btn btn-danger ms-2'>Remove Item</button>
                                </td>

                            </tr>
                        )
                    })}
                    </tbody>
                </table>
            </div>
        
        </div>
    </section>
  )
}

export default Cart```

这是我的app.js

import './App.css';
import Header from './Header'
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'
import Login from './Login'
import Home from './Home';
import Checkout from './Checkout';
import Footer from './Footer';
import Navlinks from './Navlinks';
import Cart from './Cart';



function App() {
  return (
<Router>

      <div className="App">
        <Switch>
          <Route path="/checkout">
            <Header/>
            <Checkout/>
          </Route>

          <Route path="/login">
            <Login/>
          </Route>

          <Route path="/">
            <Header/>
            <Navlinks/>
            <cartProvider>
            <Home/>
            <Cart/>
            </cartProvider>
            <Footer/>

          </Route>

        </Switch>
    
      </div>
      </Router>
  );
}

export default App;```

这是我的data.js


const data = {
    productData:[
        {
            id:1,
            img: vase,
            title:'Porcilane flower vase',
            desc: '',
            price: 46,
        },
        {
            id:1,
            img: vase,
            title:'Porcilane flower vase',
            desc: '',
            price: 46,
        },
        {
            id:1,
            img: vase,
            title:'Porcilane flower vase',
            desc: '',
            price: 46,
        },
        {
            id:1,
            img: vase,
            title:'Porcilane flower vase',
            desc: '',
            price: 46,
        },
    ],
};

export default data;```

This is the error I get every time I try and add something to my cart.

Uncaught TypeError: addItem is not a function at onClick (ItemCard.js:16:1).

I cannot figure out what I am doing wrong. I'm new to react so any help would be greatly appreciated.

This is my ItemCard.js

import {useCart} from 'react-use-cart'

const ItemCard = (props) => {
const {addItem}=useCart();

 return (
   <div className='col-11 col-md-6 col-lg-3 mx-0 mb-4'>
   <div class="card p-0 overflow-hidden h-100 shadow" >
       <img src={props.img} class="card-img-top img-fluid" alt=""/>
       <div class="card-body text-center">
           <h5 class="card-title">{props.title}</h5>
           <h5 class="card-title">${props.price}</h5>
           <p class="card-text">{props.desc}</p>
           <buttton class="btn btn-success"
            onClick={()=>addItem(props.item)}
            >Add to Cart</buttton>
       </div>
   </div>
   </div>
 )
}

export default ItemCard```

This is my Cart.js

import React from 'react'
import { useCart } from 'react-use-cart'
import ItemCard from './ItemCard';

const Cart = () => {

const {
    isEmpty,
    totalUniqueItems,
    totalItems,
    items,
    cartTotal,
    updateItemQuantity,
    removeItem,
    emptyCart,
    }=useCart();
    if(isEmpty) return <h1 className='text-center'>Your Cart is Empty</h1>

  return (
      <section className='py-4 container'>
        <div className='row justify-content-center'>
            <div className='col-12'>
                <h5>Cart ({totalUniqueItems}) total Items:({totalItems})</h5>
                <table className='table table-light table-hover m-0'>
                    <tbody>
                    {items.map((item,index)=>{
                        return(
                            <tr key={index}>
                                <td>
                                    <img src={item.img} style={{height:'6rem'}}/>
                                </td>
                                <td>{item.title}</td>
                                <td>{item.price}</td>
                                <td>Quantity ({item.quantity})</td>
                                <td>
                                    <button className='btn btn-info ms-2'
                                     onClick={()=>updateItemQuantity(item.id, item.quantity - 1)}
                                    >-</button>
                                    <button className='btn btn-info ms-2'
                                    onClick={()=>updateItemQuantity(item.id, item.quantity + 1)}
                                    >+</button>
                                    <button className='btn btn-danger ms-2'>Remove Item</button>
                                </td>

                            </tr>
                        )
                    })}
                    </tbody>
                </table>
            </div>
        
        </div>
    </section>
  )
}

export default Cart```

This is my App.js

import './App.css';
import Header from './Header'
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'
import Login from './Login'
import Home from './Home';
import Checkout from './Checkout';
import Footer from './Footer';
import Navlinks from './Navlinks';
import Cart from './Cart';



function App() {
  return (
<Router>

      <div className="App">
        <Switch>
          <Route path="/checkout">
            <Header/>
            <Checkout/>
          </Route>

          <Route path="/login">
            <Login/>
          </Route>

          <Route path="/">
            <Header/>
            <Navlinks/>
            <cartProvider>
            <Home/>
            <Cart/>
            </cartProvider>
            <Footer/>

          </Route>

        </Switch>
    
      </div>
      </Router>
  );
}

export default App;```

This is my data.js


const data = {
    productData:[
        {
            id:1,
            img: vase,
            title:'Porcilane flower vase',
            desc: '',
            price: 46,
        },
        {
            id:1,
            img: vase,
            title:'Porcilane flower vase',
            desc: '',
            price: 46,
        },
        {
            id:1,
            img: vase,
            title:'Porcilane flower vase',
            desc: '',
            price: 46,
        },
        {
            id:1,
            img: vase,
            title:'Porcilane flower vase',
            desc: '',
            price: 46,
        },
    ],
};

export default data;```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇女薄笑 2025-01-30 03:35:49

在app.js中添加

import {cartProvider}来自'react-use-cart';

并扭曲所有内容

<CartProvider> <CartProvider/>

import { CartProvider } from 'react-use-cart';
import './App.css'
import ListContainer from "./component/ListContainer";

function App() {
  return (
    <>
    <CartProvider>
      <ListContainer/>
    </CartProvider>
    </>
  );
}

export default App;

In App.js Add

import { CartProvider } from 'react-use-cart';

and warp all in

<CartProvider> <CartProvider/>

For example :

import { CartProvider } from 'react-use-cart';
import './App.css'
import ListContainer from "./component/ListContainer";

function App() {
  return (
    <>
    <CartProvider>
      <ListContainer/>
    </CartProvider>
    </>
  );
}

export default App;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文