React.js:无法访问状态对象内的对象属性

时间:2020-03-02 01:48:39

标签: reactjs object state

我正在研究财务追踪器。最终,财务跟踪器将能够访问用户银行帐户中的帐户,提取每笔交易,并且用户将能够添加交易作为未来的预测。想法是使用户能够使用用户银行帐户中的最新支票/储蓄帐户信息来运行财务预测/方案。

我正在研究“运行总额”列,该列将使用在transactionData.amount中找到的金额,并且如果这是“零”索引,则会将transactionData.amount添加到startBal中。否则,它将使用在transactionData.runningTotal的上一个索引中找到的数字,并添加到对transactionData.amount的当前索引中找到的值。

无论哪种情况,都应将新的计算添加到transactionData.runningTotal的当前索引中。如果银行尚未提供该数据,我实质上是在模仿在线交易的详细信息。

这是父组件。

import React, { Component } from "react";

import TransactionSearch from "./transactionSearch.js";
import PendingTransactions from "./pendingTransactions.js";
import Transactions from "./transactions.js";

class CheckingAccount extends Component {
  state = {
    startBal: 1000,
    pendingTransData: [
      { id: 0, date: "1/1/2020", transaction: "gas", amount: -25.45 },
      { id: 1, date: "1/2/2020", transaction: "cell phone", amount: -127.35 },
      { id: 2, date: "1/3/2020", transaction: "car payment", amount: -303.97 }
    ],
    transactionData: [
      {
        id: 0,
        date: "1/1/2020",
        transaction: "gas",
        amount: -35.45,
        runningTotal: null
      },
      {
        id: 1,
        date: "1/2/2020",
        transaction: "cell phone",
        amount: -227.35,
        runningTotal: null
      },
      {
        id: 2,
        date: "1/3/2020",
        transaction: "car payment",
        amount: -403.97,
        runningTotal: null
      }
    ]
  };

  addRunningTotal() {
    let { transactionData, startBal } = this.state;
    console.log(transactionData);

    transactionData.map((el, i) => {
      console.log("in map function");
      if (el[i] === 0) {
        return (el[i].runningTotal = el[i].amount + startBal);
      } else if (el[i] > 0) {
        return (el[i].runningTotal = el[i - 1].amount + el[i].amount);
      }
    });
    console.log("out of map function");
    console.log("start Balance: ", startBal);
    console.log("amount: ", transactionData[0].amount);
    console.log("running total: ", transactionData[0].runningTotal);

    this.setState({ transactionData: transactionData, startBal: startBal });
  }

  componentDidMount() {
    this.addRunningTotal();
  }

  render() {
    let pendTransData = (
      <div>
        <h1>PendingTransactions</h1>
        <table>
          <tr>
            <th>Date</th>
            <th>Transaction</th>
            <th>Amount</th>
          </tr>
        </table>
        {this.state.pendingTransData.map((pendingTransData, index) => {
          return (
            <PendingTransactions
              key={pendingTransData.id}
              date={pendingTransData.date}
              transaction={pendingTransData.transaction}
              amount={pendingTransData.amount}
            />
          );
        })}
      </div>
    );

    let transData = (
      <div>
        <h1>Transaction Component</h1>
        <table>
          <tr>
            <th>Date</th>
            <th>Transaction</th>
            <th>Amount</th>
            <th>Running Total</th>
          </tr>
        </table>
        {this.state.transactionData.map((transactionData, index) => {
          return (
            <Transactions
              key={transactionData.id}
              date={transactionData.date}
              transaction={transactionData.transaction}
              amount={transactionData.amount}
              runningTotal={transactionData.runningTotal}
            />
          );
        })}
      </div>
    );

    return (
      <div className="App">
        <h1> Checking Account</h1>
        <TransactionSearch />
        {pendTransData}
        {transData}
      </div>
    );
  }
}

export default CheckingAccount;

此处是应显示数据的子组件。

import React from "react";

function Transactions(props) {
  return (
    <tr>
      <td>{props.date} </td>
      <td>{props.transaction}</td>
      <td>{props.amount}</td>
      <td>{props.runningTotal}</td>
    </tr>
  );
}

export default Transactions;

首先,runningTotal属性不会在组件中呈现。我希望在runningTotal属性中看到包含新数据的列。

1 个答案:

答案 0 :(得分:1)

在addRunningTotal中,看起来就像您使用map一样。在map((el, i) => {})中,el是对当前迭代值的引用,因此在使用el[i](未定义)的地方,您只想使用el

您还只需要在if语句中使用i(索引)。

这应该可以解决问题(保持对先前值的引用):

let prevAmount, running;
transactionData.map((el, i) => {
  if (i === 0) {
    running = el.runningTotal = el.amount + startBal;
    prevAmount = el.amount;

    return running;
  } else if (i > 0) {
    running = el.runningTotal = prevAmount + el.amount;
    prevAmount = el.amount;

    return running;
  }
});