我想加载组件,但它显示了一个问题,即具有相同密钥的组件,但我映射到了selectedGroups上,并将gr.id作为密钥提供给它,它在第一个版本之前是有效的
sharingTabs = selectedGroups.map(gr => (
<ExpansionPanel>
<ExpansionPanelSummary expandIcon={<ExpandMoreIcon />}>
<Typography className={classes.heading}>{gr.name}</Typography>
</ExpansionPanelSummary>
<ExpansionPanelDetails>
<Grid container spacing={16}>
<Grid item xs>
<SharingSpacesTabs />
</Grid>
</Grid>
</ExpansionPanelDetails>
</ExpansionPanel>
));
但是后来我想在一个道具中发送索引,这就是为什么我在map中添加了另一个map,这就是导致回调函数问题的原因,然后我返回
sharingTabs = selectedGroups.map(function(gr) {
const indexs = groups.map((group, index) => {
if ((group.sharingspace.element = gr.id)) {
return index;
}
});
return (
<ExpansionPanel key={gr.id}>
<ExpansionPanelSummary expandIcon={<ExpandMoreIcon />}>
<Typography className={classes.heading}>{gr.name}</Typography>
</ExpansionPanelSummary>
<ExpansionPanelDetails>
<Grid container spacing={16}>
<Grid item xs>
<SharingSpacesTabs id={gr.id} index={indexs[0]} />
</Grid>
</Grid>
</ExpansionPanelDetails>
</ExpansionPanel>
);
});
你能帮我找到一个我需要的解决方案吗?谢谢
发布于 2019-06-05 03:17:24
我认为您的条件有问题,请更改您的函数条件以正确检查与==或===的等价性。
我也不确定如果条件不满足,你想做什么,map将为每个不满足条件的元素返回null。
const indexs = groups.map((group, index) => {
if ((group.sharingspace.element == gr.id)) {
return index;
}
});
发布于 2019-06-05 02:43:07
最好的方法是在map中传递第二个index
作为第二个参数,并将key={index}
sharingTabs = selectedGroups.map((gr, index) => {
const indexes = groups.map((group, index) => {
if ((group.sharingspace.element = gr.id)) {
return index;
}
});
return (
<ExpansionPanel key={index}>
<ExpansionPanelSummary expandIcon={<ExpandMoreIcon />}>
<Typography className={classes.heading}>{gr.name}</Typography>
</ExpansionPanelSummary>
<ExpansionPanelDetails>
<Grid container spacing={16}>
<Grid item xs>
<SharingSpacesTabs id={gr.id} index={indexes[0]} />
</Grid>
</Grid>
</ExpansionPanelDetails>
</ExpansionPanel>
);
});
https://stackoverflow.com/questions/56449489
复制相似问题