问题描述
我正在尝试访问数组的不同索引以在我的一个组件中显示,但我遇到了问题。这是我目前使用的代码:
const mapStatetoProps = (state) => {
return {
weight: state.move[0].movementWeight,name: state.move[0].movementName
}
};
const MovementButton = (props) => {
const classes = useStyles();
const mapNames = props.name.map((lift) => {
return (
<Button
key={lift.movementName}
className={classes.movementButtons}
onClick={() => history.push(`/movement/${lift.movementName}/${lift.movementWeight}`)}
>
{lift.movementName} - {lift.movementWeight}lbs
</Button>
)
});
const displayMovementButtons = () => {
if (mapNames.length === 0) {
return <div className={classes.noMovementsMessage} >Click add button to begin</div>
};
return <div>{mapNames}</div>
};
return <div className={classes.movementButtonsDiv}>{displayMovementButtons()}</div>
};
const mapStatetoProps = (state) => {
return {
name: state.move
}
};
export default connect(mapStatetoProps)(MovementButton);
但这使得 props.weight 始终是 0 索引,而有时我需要索引 1、2 等。我使用 [0] 的原因是因为没有它,我会变得未定义。 有什么帮助吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)