简体   繁体   English

为 Cart 组件添加移除按钮

[英]Add a remove button for Cart component

I have the "Cart" component using react-redux and want to implement a "Remove" button in the component!我有一个使用 react-redux 的“Cart”组件,并想在组件中实现一个“Remove”按钮! I tried create the function within however when I click it "products" are not be removed and returning an error: "product is undefined".我尝试在其中创建函数,但是当我单击它时,“产品”不会被删除并返回错误:“产品未定义”。 I have alerady a remove button in another component where using redux the product been removed so it works, however I want to have the button as well in the "Cart" component its self to be able to see the products that been removed!我已经在另一个组件中发现了一个删除按钮,使用 redux 删除了产品,因此它可以工作,但是我希望在“购物车”组件中也有按钮,以便能够看到被删除的产品! My cart component code is the following:我的购物车组件代码如下:

import React, { Component } from "react"
import { connect } from "react-redux";
import { Card, CardBody, CardHeader, CardTitle, Row, Col } from "reactstrap"
import PanelHeader from "components/PanelHeader/PanelHeader.js"
import { addCart } from "../redux/actions";
import { removeCart } from "../redux/actions";


class Cart extends Component {
  render () {
    const cart = this.props.cart
    return (
      <>
        <PanelHeader size="sm" />
        <div className="content">
          <Row>
            <Col xs={12}>
              <Card>
                <CardHeader>
                  <CardTitle tag="h4">Products List</CardTitle>
                </CardHeader>
                <CardBody>
                <table class="table table-striped table-hover">
                  <thead>
                    <tr>
                      <th scope="col"><strong>#</strong></th>
                      <th scope="col"><strong>Name</strong></th>
                      <th scope="col"><strong>Code Item</strong></th>
                      <th scope="col"><strong>Quantity</strong></th>
                      <th scope="col"><strong>Price Total</strong></th>
                    </tr>
                  </thead>
                    <tbody>
                      {cart.length > 0 && cart.map((cart, index) => (             
                      <tr key={cart.id}>
                    <th scope="row">{index}</th>
                    <td>{cart.title}</td>
                    <td>{cart.code}</td>
                    <td>{cart.quantity}</td>
                    <td>{cart.price}</td>
                      </tr>))}
                      <span>
                       <button onClick ={() => (removeCart(product))}>Remove</button>
                      </span>
                    </tbody>
                </table>
              </CardBody>
            </Card>
          </Col>
        </Row>
      </div>
    </>
    )
  }
}

const mapStateToProps = (state)=> {
  return {
      cart: state.cart
       }
  }

const mapDispatchToProps = (dispatch) => { 
      return {
        addCart: (product) => {dispatch(addCart(product))},
        removeCart: (product) => {dispatch(removeCart(product))}
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(Cart);

Thank you in advance!!!先感谢您!!!

产品未定义,因为您的组件中没有任何名为产品的内容,您尚未声明它,如果您想删除整个购物车,则发送购物车的 id 而不是(产品),如果您想删除购物车内的特定产品,那么您可能会发送特定产品的 ID,而不是整个产品对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM