gpt4 book ai didi

javascript - 为什么 map 未定义?

转载 作者:行者123 更新时间:2023-12-02 23:04:27 25 4
gpt4 key购买 nike

我是 redux 的新手,我正在尝试将其合并到我的代码中,以便可以更轻松地管理状态。我正在尝试映射我所在州的数组,但它抛出以下错误:

Uncaught TypeError: Cannot read property 'map' of undefined

这是 redux 的 reducer

const initialState = {
workoutlist: [
{
id: uuid.v4(),
name: 'Leg Day',
date: '08-09-2019',
duration: "60",
exerciselist: [
{
id: uuid.v4(),
exerciseName: 'Squats',
numberOfSets: "3",
reps: "12",
weight: "135",
},
{
id: uuid.v4(),
exerciseName: 'Leg press',
numberOfSets: "3",
reps: "10",
weight: "150",
},
{
id: uuid.v4(),
exerciseName: 'Lunges',
numberOfSets: "4",
reps: "12",
weight: "0",
},
],
selected: false,
},
{
id: uuid.v4(),
name: 'Running',
date: '08-11-2019',
duration: "40",
exerciselist: [],
selected: false,

},
],
disabled: true,
page: 1,
}

const workoutList = (state = initialState, action) => {
switch(action.type) {

// Returns whether the panel is selected or not
// and enables Start Workout button if only 1 is selected
case 'SELECT_PANEL':
state = {
workoutlist: state.workoutlist.map(workout => {
if (workout.id === action.id) {
workout.selected = !workout.selected
}
return workout;
})
}
var count = 0;
state.workoutlist.map((workout) => {
if (workout.selected === true) {
count = count + 1;
}
return count;
})
if (count !== 1) {
state = {
...state,
disabled: true
}
} else {
state = {
...state,
disabled: false
}
}
return state;

default:
return state;
}
}

这是抛出错误的组件。

export default function WorkoutItem() {
const handleSelectedPanel = useSelector(state => state.workoutList);
const dispatch = useDispatch();
const { name, date, duration, exerciselist } = handleSelectedPanel;
return (
<ExpansionPanel style={styles.panel} onChange={() => dispatch(selectPanel())}>
<ExpansionPanelSummary>
<Typography variant="button" style={styles.header}>
{name}
</Typography>
<Typography variant="button" style={styles.header}>
{date}
</Typography>
<Typography align="right" style={styles.header}>
~{duration} mins
</Typography>
</ExpansionPanelSummary>
<ExpansionPanelDetails>
<Table size="medium" style={styles.table}>
<TableHead>
<TableRow>
<TableCell padding="none" >Name</TableCell>
<TableCell padding="none" align="right"># of sets</TableCell>
<TableCell padding="none" align="right">average reps</TableCell>
<TableCell padding="none" align="right">weight</TableCell>
</TableRow>
</TableHead>
<TableBody>
{exerciselist.map((exercise) => (
<ExerciseList
key={exercise.id}
exercise={exercise}
/>
))}
</TableBody>
</Table>
<ExpansionPanelActions disableSpacing style={styles.actionButton}>
<EditWorkoutItem
workout={this.props.workout}
handleEditChange={this.props.handleEditChange}
/>
</ExpansionPanelActions>
</ExpansionPanelDetails>
</ExpansionPanel>
)
}

它应该显示一个可以扩展以显示内容的面板列表,但它会抛出一个错误,表明锻炼列表在我的状态下显然是未定义的。

任何帮助将不胜感激!

最佳答案

您想要访问第一项还是 workoutList 吗?从您的代码 handleSelectedPanel 接缝是一个数组而不是一个对象。你的解构正确吗?你的useSelector在哪里? console.log(handleSelectedPanel) 为您提供了什么?尝试一下

const { name, date, duration, exerciselist } = handleSelectedPanel[0];

const handleSelectedPanel = useSelector(state => state.workoutList[0]);

关于javascript - 为什么 map 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57651717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com