Redux configureStore新增reducer后出现map is not a function报错求助
问题原因
你调整store的reducer配置后,Redux根state的结构发生了变化:
- 旧配置直接把
todoReducer传给reducer字段时,整个Redux根state就是todoReducer返回的todos数组 - 新配置传入了包含多reducer的对象后,Redux根state变成了对象结构:
{ todo: todos数组, note: notes相关数据 }
你现在Todos组件里获取todos的代码还是按旧结构取的,所以拿到的是整个根state对象,不是数组,调用map方法自然报错。
解决方案
找到你Todos组件里获取todos的逻辑,修改取值路径即可:
- 如果你用的是
useSelector钩子:
修改前的代码大概率是:
const todos = useSelector(state => state)
修改为:
const todos = useSelector(state => state.todo)
- 如果你用的是
connect高阶组件:
修改前mapStateToProps的代码大概率是:
const mapStateToProps = state => ({ todos: state })
修改为:
const mapStateToProps = state => ({ todos: state.todo })
内容的提问来源于stack exchange,提问作者learningbtw
相关产品推荐
相关产品推荐

