[英]React.js: Unable to access object attribute within state object
I am working on a finance tracker.我正在开发财务跟踪器。 Eventually, the finance tracker will be able to access an account in the user's bank account, pull every transaction, and the user will be able to add transactions as future projections.
最终,财务跟踪器将能够访问用户银行账户中的账户,提取每笔交易,并且用户将能够添加交易作为未来的预测。 The idea is to give the user the ability to run financial projections/scenarios using the most recent checking/saving account information in the user's bank account(s).
这个想法是让用户能够使用用户银行账户中最近的支票/储蓄账户信息来运行财务预测/方案。
I am working on a "running total" column which takes the amount found in transactionData.amount
and will add transactionData.amount
to startBal
if this is the "zero" index.我正在处理一个“运行总计”列,它采用在
transactionData.amount
找到的transactionData.amount
,如果这是“零”索引,则会将transactionData.amount
添加到startBal
。 Otherwise, it will use the numbers found in the previous index for transactionData.runningTotal
and add to the value found in the current index for transactionData.amount.否则,它将使用在
transactionData.runningTotal
的先前索引中找到的数字,并添加到在 transactionData.amount 的当前索引中找到的值。
In either case, the new calculation should be added to the current index for transactionData.runningTotal.在任一情况下,新计算都应添加到 transactionData.runningTotal 的当前索引中。 I am essentially mimicking what an online transaction detail would, in the event that the bank does not provide this data already.
如果银行尚未提供此数据,我基本上是在模仿在线交易详细信息。
Here is the parent component.这是父组件。
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;
Here is the child component where the data should appear.这是数据应该出现的子组件。
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;
First, runningTotal
attribute does not render in the component.首先,
runningTotal
属性不会在组件中呈现。 I expected to see a column with the new data in the runningTotal
attribute.我希望在
runningTotal
属性中看到包含新数据的列。
In addRunningTotal, It looks like it's how you've used map.在 addRunningTotal 中,看起来这就是您使用地图的方式。 In
map((el, i) => {})
, el
is a reference to the current iteration's value so where you've used el[i]
(undefined), you wanted to use just el
.在
map((el, i) => {})
, el
是对当前迭代值的引用,因此在您使用el[i]
(未定义)的地方,您只想使用el
。
You also only need to use i
(index) in your if statement.您还只需要在 if 语句中使用
i
(索引)。
This should do the trick (keeping reference to the previous value):这应该可以解决问题(保持对先前值的引用):
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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.