【发布时间】:2021-03-09 21:01:38
【问题描述】:
我正在使用 react js 并尝试从 holdingsByAccounts 属性中访问一些虚拟数据。页面加载时,问题 jsx 未呈现到屏幕上。一直在寻找其他方式和其他有这种情况的网站。所以我的代码如下:
class PortDashSideBar extends Component {
constructor(props) {
super(props);
this.oldPortfolioValue = 0;
}
getHoldingsForAccount = (id) => {
this.props.holdingsByAccount.map((holding) => {
return <span>{holding[id].value}</span>;
});
}
renderConfigurations = () => {
const {
classes,
accounts,
holdingsByAccount,
holdingsByAsset,
currentAccountId,
actions,
user
} = this.props;
const animationClassGreen = 'animateChangeGreen';
const animationClassRed = 'animateChangeRed';
return (
<Table className={classes.table} name="myConfigurations">
<TableBody style={{ display: 'flex', flexDirection: 'column' }}>
{
accounts.map(account => (
<ButtonBase>
<TableRow
hover
name={account.name}
key={account.id}
onClick={() => { actions.updateAccount(account.id); }}>
<TableCell className={classes.cellBotIcon} />
<TableCell className={classes.cellName}>
<Typography className={classes.botName}>
{account.label}
</Typography>
<Typography>
<svg className={classes.svg}>
<img src="https://yt3.ggpht.com/ytc/AAUvwngo7rox3GTqcW5Omxr-UGXHwmGO4To3QDygqaYxpg=s900-c-k-c0x00ffffff-no-rj" />
</svg>
<span className={classes.configName}>
{this.getHoldingsForAccount(account.id)}
</span>
</Typography>
</TableCell>
</TableRow>
</ButtonBase>
))
}
</TableBody>
</Table>
);
}
render() {
const {
classes
} = this.props;
return (
<Paper className={classes.paper} elevation={0} square >
<Grid container alignItems="center" justify="center">
<Grid className={classes.configurations} xs={12} item>
{this.renderConfigurations()}
</Grid>
</Grid>
</Paper>
);
}
}
PortDashSideBar.defaultProps = {
};
PortDashSideBar.propTypes = {
actions: PropTypes.object.isRequired,
classes: PropTypes.object.isRequired,
accounts: PropTypes.array.isRequired,
holdingsByAccount: PropTypes.array.isRequired,
holdingsByAsset: PropTypes.array.isRequired,
currentAccountId: PropTypes.string.isRequired,
user: PropTypes.object.isRequired
};
function mapStateToProps(state) {
return {
user: state.global.user.user,
holdingsByAccount: state.holdings.holdings.byAccount,
holdingsByAsset: state.holdings.holdings.byAsset,
holdingsLoaded: state.holdings.holdings.holdingsLoaded,
accounts: state.global.accounts.accounts,
currentAccountId: state.trade.interactions.currentAccountId,
};
}
function mapDispatchToProps(dispatch) {
return {
actions: {
...bindActionCreators({
updateAccount,
}, dispatch)
}
};
}
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(PortDashSideBar));
现在,它没有显示错误,但也没有在屏幕上呈现
【问题讨论】:
-
您确定
holdingsByAccount中的每个持股对accounts中的每个帐户都有价值吗? -
是的,我刚刚检查过
-
你需要更改getHoldingsForAccount方法,你应该通过id找到记录,你试图通过索引找到它,试试this.props.holdingsByAccount.find(({id : holdingId }) => holdingId === id),这样你就可以渲染正确的持有物品了。
标签: javascript json reactjs react-native asynchronous