React/Redux报错TypeError: elements is not iterable,elements与connections为undefined排查
问题原因
- 核心原因是Redux状态层级访问错误:你定义的
flowreducer如果是通过combineReducers挂载到根状态的,那么它内部的elements、connections状态都存放在根状态的flow字段下,你当前的mapStateToProps直接从根状态取这两个字段,自然获取不到值返回undefined。 - 次要问题是PropTypes类型定义错误:
elements和connections都是数组类型,你声明为了PropTypes.object,会触发类型校验警告。
修复方案
- 修改
mapStateToProps的状态取值路径,正确从flow子状态下取对应字段:
const mapStateToProps = (state) => ({ elements: state.flow.elements, connections: state.flow.connections, });
如果不确定根状态的结构,可以在mapStateToProps中打印完整的state变量查看层级。
- 修正PropTypes的类型定义:
NavigationBar.propTypes = { saveData: PropTypes.func.isRequired, elements: PropTypes.array.isRequired, connections: PropTypes.array.isRequired, };
- 确认根reducer的注册逻辑,确保
flowreducer被正确挂载:
// 根reducer示例代码 import { combineReducers } from 'redux'; import flow from './flow'; export default combineReducers({ flow, // 其他业务reducer });
内容的提问来源于stack exchange,提问作者Oussama Bouchikhi
相关产品推荐
相关产品推荐

