Redux-从状态删除项目

时间:2018-09-20 18:53:50

标签: javascript reactjs redux state

我在从状态中删除项目时遇到问题。

我有一个容器发票,其中显示了在 InvoiceItem 中呈现的发票列表。

我正在使用mapStateToProps和一个操作/缩减程序关闭并获取所有发票。

然后,我在InvoiceItem中使用mapDispatchToProps,对删除按钮进行操作。 (仅从状态中删除,而不是从数据库中删除。)

这是处理Redux操作(连接)的正确用法吗?通过使用React16的Context API,我知道这就是我的处理方式。

在action和reducer文件中使用console.log调试mapDispatchToProps时,有效负载以UNDEFINED的形式传入

下面是我的代码:

types.js

export const GET_INVOICES = "GET_INVOICES";
export const GET_INVOICE = "GET_INVOICE";
export const DELETE_INVOICE = "DELETE_INVOICE";
export const INVOICES_LOADING = "INVOICES_LOADING";

invoiceActions.js

import axios from "axios";
import { GET_INVOICES, DELETE_INVOICE, INVOICES_LOADING } from "./types";

// getInvoices is a large axios call that goes here. It works fine.
// Leaving it out in this example.

// Delete Invoice
export const deleteInvoice = invoice => dispatch => {
 console.log(invoice) // UNDEFINED
dispatch({
  type: DELETE_INVOICE,
  payload: invoice
});
};

// Invoices loading
export const setInvoicesLoading = () => {
 return {
  type: INVOICES_LOADING
 };
};

invoicesReducer.js

import {
 GET_INVOICES,
 DELETE_INVOICE,
 INVOICES_LOADING
 } from "../actions/types";

const initialState = {
invoices: [],
loading: false
};

 export default function(state = initialState, action) {
  switch (action.type) {
    case INVOICES_LOADING:
     return {
      ...state,
      loading: true
     };
   case GET_INVOICES:
     return {
     ...state,
     invoices: action.payload,
     loading: false
    };
   case DELETE_INVOICE:
    return invoices.filter(invoice => invoice.Id !== invoice);
    default:
    return state;
   }
  }

InvoiceItem.js

import React, { Component } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { deleteInvoice } from "../../actions/invoiceActions";

class InvoiceItem extends Component {
 onDeleteClick(e) {
 this.props.deleteInvoice();
 }
 render() {
  const { invoice } = this.props;

    return (
     <div className="card card-body bg-light mb-3">
       <div className="row">
         <div className="col-lg-6 col-md-4 col-8">
           <h3>Invoice: {invoice.InvoiceNumber}</h3>
           <p>Client: {invoice.Contact.Name}</p>
           <button
            onClick={this.onDeleteClick.bind(this)}
            className="btn btn-danger">
            Remove
           </button>
      </div>
    </div>
  </div>
  );
  }
 }

 InvoiceItem.propTypes = {
 invoice: PropTypes.object.isRequired,
 deleteInvoice: PropTypes.func
 };

 const mapDispatchToProps = dispatch => {
   return {
     deleteInvoice: invoice => dispatch(deleteInvoice(invoice))
    };
  };

  export default connect(
   mapDispatchToProps,
   { deleteInvoice }
  )(InvoiceItem);

Invoices.js

import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import Spinner from "../common/Spinner";
import InvoiceItem from "./InvoiceItem";
import { getInvoices } from "../../actions/invoiceActions";

class Invoices extends Component {
  componentDidMount() {
  this.props.getInvoices();
}

render() {
  const { invoices, loading } = this.props.invoice;
  let invoiceItems;

  if (invoices === null || loading) {
   invoiceItems = <Spinner />;
  } else {
  if (invoices.length > 0) {
    invoiceItems = invoices.map(invoice => (
      <InvoiceItem key={invoice.Id} invoice={invoice} />
    ));
  } else {
    invoiceItems = <h4>No invoices found...</h4>;
  }
}

return (
  <div className="profiles">
    <div className="container">
      <div className="row">
        <div className="col-md-12">
          <h1 className="display-4 text-center">Invoices</h1>
          {invoiceItems}
        </div>
      </div>
    </div>
  </div>
  );
 }
}

Invoices.propTypes = {
 getInvoices: PropTypes.func.isRequired,
 invoice: PropTypes.object.isRequired
};

const mapStateToProps = state => ({
 invoice: state.invoices
});


export default connect(
 mapStateToProps,
 { getInvoices }
)(Invoices);

1 个答案:

答案 0 :(得分:3)

deleteInvoice期望传递invoice对象。由于您当前未传递任何内容,因此它记录undefnied

onDeleteClick(e) {
   this.props.deleteInvoice(this.props.invoice);
}