javascript - 删除每个项目后为值显示 null
问题描述
我正在使用反应,我已经实现了一个删除功能。该功能应该删除该项目。它完成了删除项目的工作,但是,在删除每个项目后,显示的值都将显示为空。我认为这是没有呈现选定项目(在这种情况下为帐户项目)的结果。我想知道如何默认使用帐户项目或执行类似于默认设置的操作。我试过做 componentDidMount 并在那里设置帐户。然而,它没有做这项工作。请看我的代码。谢谢
import React, { Component, Fragment } from 'react';
import { Typography, withStyles } from '@material-ui/core';
import PropTypes from 'prop-types';
import Paper from '@material-ui/core/Paper';
import Grid from '@material-ui/core/Grid';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import MenuItem from '@material-ui/core/MenuItem';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import AddAccount from '../common/addAccount';
import { updateAccount } from '../../store/ducks/trade/interactions';
import PortDashFaq from './portDashFaq';
import AccountTypeIcon from './AccountTypeIcon';
import Formatted from './formatted';
import { getChangeColor, formatChangePct } from '../../utils/helpers';
import DeleteAccount from './../common/deleteAccount';
getHoldingsForAccount = (id) => {
const {
theme
} = this.props;
const { pref_currency: prefCurrency } = this.props.user.preferences;
const holding = this.props.holdingsByAccount.find(item => item.id === id);
return holding && (
<div style={{ width: '100%', display: 'flex', flexDirection: 'row' }}>
<Typography color="textSecondary" className={this.props.classes.value} name="portfolioValue">
<Formatted convertToPref asset={prefCurrency} amount={holding.value} />
</Typography>
<Typography className={this.props.classes.value} style={{ color: getChangeColor(holding.change24h, theme), paddingLeft: '5px' }} name="24HourChange">
{`(${formatChangePct(prefCurrency, holding.percentChange)})`}
</Typography>
</div>
);
}
getAccountById = (id) => {
const { accounts } = this.props;
return accounts.find(a => a.id === id);
}
setCurrentAccount = (id) => {
const { actions } = this.props;
actions.updateAccount(id);
}
switchView = (event) => {
if (this.props.currentAccountId !== event.target.value) {
const id = event.target.value !== 'aggregate' ? event.target.value : '';
this.props.actions.updateAccount(id);
}
};
handleMouseEnter = (id) => {
this.setState({
hoverId: id,
isHovering: true,
});
}
handleMouseLeave = () => {
this.setState({
isHovering: false
});
}
renderAccounts = () => {
const {
classes,
holdingsByAccount,
currentAccountId,
user,
theme
} = this.props;
const { pref_currency: prefCurrency } = user.preferences;
const portfolioSummary = this.aggregateHoldings('');
console.log(this.props)
{
holdingsByAccount.map(account => (
<MenuItem
className={account.id === currentAccountId ? classes.selectedAccount : classes.unselectedAccount}
hover
onMouseEnter={() => this.handleMouseEnter(account.id)}
onMouseLeave={() => this.handleMouseLeave()}
name={account.name}
key={account.id}
onClick={ () => this.setCurrentAccount(account.id) }>
<TableCell className={classes.cellAccountIcon}>
<AccountTypeIcon type={(this.getAccountById(account.id) || {}).type} />
</TableCell>
<TableCell className={classes.cellName}>
<Typography className={classes.accountName}>
{account.label}
</Typography>
<Typography style={{ display: 'flex', justifyContent: 'space-between' }}>
{this.getHoldingsForAccount(account.id)}
<div className={classes.deleteIcon}>
{this.state.isHovering && this.state.hoverId === account.id ? <DeleteAccount key={account.id} setCurrentAccount={this.setCurrentAccount} accountId={account.id} accountLabel={account.label} /> : '' }
</div>
</Typography>
</TableCell>
</MenuItem>
))
}
</TableBody>
</Table>
);
}
PortDashSideBar.defaultProps = {
currentAccountId: ''
};
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles, { withTheme: true })(PortDashSideBar));
解决方案
推荐阅读
- python - 无符号字符数组问题
- c++ - 如何避免文件变量成为 OpenMP 中的竞争条件(离散元方法模拟)
- php - 将命令行 cURL 转换为 PHP cURL(带身份验证)
- laravel - Laravel 如何测试 _Constructor
- swift - 如何正确获取用户坐标?
- html - 五个盒子,四个在角落,一个在中心
- linux - 如何获得本月的最后一天 Shell Scripting
- apache-kafka - 多个分区的Kafka本地状态存储
- angular - 通过存储的 Firebase 首选项过滤 Ionic 卡
- reactjs - 如何在 FullCalendar v4 中仅选择可见事件