以下是我的要求:
这里像“保险”一样,有不同种类的块,并且在每个块中可以添加任意数量的记录。在添加每一行时,我将总计更新为“表标题”,并将所有表标题的总和更新为“总投资”。
我的组件是这样的。
File1.js(主要组件)
render:
<TotalInvestment amountMap={this.state.totalAmounts}/>
<TableComponent finalTotalAmount={_self._getFinalTotalAmount}/>
_getFinalTotalAmount = (type, amount) => {
if((!this.state.totalAmounts.get(type) && this.state.totalAmounts.get(type) != 0) ||
this.state.totalAmounts.get(type) !== amount) {
this.setState(() => ({totalAmounts: this.state.totalAmounts.set(type, amount)}));
}
}
File2.js(表组件)
render: <div class="table-head"> <span id="amount"> Total: {this.state.totalAmt}/- </span></div>
<table class="table">
<thead class="table-bordered">{tableHeader}</thead>
<tbody>{dataRecord}</tbody>
</table>
//It get called when any new row is added.
_getTotalAmount = (amount) => {
var totalAmount = parseFloat(this.state.totalAmt || 0 ) + parseFloat(amount || 0);
var newState = this.state;
newState.totalAmt = totalAmount.toFixed(2);
this.setState(newState);
this.props.finalTotalAmount(this.props.type, this.state.totalAmt);
}
但是,当我尝试更新“总投资”组件时,它将处于不一致状态。我在哪里做错了?如果需要,我可以添加更多信息。