【发布时间】:2018-08-18 05:20:26
【问题描述】:
我在我的应用程序中使用 Redux。
我已经在 reducer 中设置了初始状态,但显然有些地方不正确,因为我收到以下错误:
Uncaught TypeError: Cannot读取未定义的属性“地图”
这是减速器:
const initialState = {
machines: [
{
id: "1",
identifier: "My Machine 1",
location: "My Location 1"
}
]
};
export default function machines(state=initialState, action) {
switch (action.type) {
default:
return state;
}
}
这是组件:
import React, { Component } from 'react';
import {connect} from 'react-redux';
class Machines extends Component {
createMachinesList() {
return this.props.machines.map((machine) => {
return (
<table>
<tbody>
<tr>
<td key={machine.id}>{machine.identifier}</td>
</tr>
<tr>
<td key={machine.id}>{machine.location}</td>
</tr>
</tbody>
</table>
);
});
}
render() {
return (
<div>
<div className="card">
<div className="card-header">
Equipment
</div>
<div className="card-content">
{this.createMachinesList()}
<button>edit</button><button>delete</button>
</div>
</div>
</div>
)
}
}
const mapStateToProps = state => {
return {
equipment: state.machines,
}
}
const mapDispatchToProps = dispatch => {
return {
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Machines);
这里是 reducers/index.js
import { combineReducers } from 'redux';
import machines from "./machines";
const allReducers = combineReducers({
machines,
})
export default allReducers;
在 index.js 中
let store = createStore(allReducers);
ReactDOM.render(
<Provider store={store}>
<HashRouter>
<Switch>
{indexRoutes.map((prop, key) => {
return <Route to={prop.path} component={prop.component} key={key} />;
})}
</Switch>
</HashRouter>
</Provider>,
document.getElementById("root")
);
编辑:
我已将 mapStatetoProp 中的“设备”替换为“机器”,如下所示。我现在得到一个不同的错误:
Uncaught TypeError: this.props.machines.map is not a function
我需要改变什么?我被困在这个问题上。
非常感谢您!
【问题讨论】:
-
我们可以看看你的 store 和 reducer 配置吗?以下答案之一可以解决您的问题,但可以肯定的是,提供该信息会很好。
-
@devserkan 编辑了我的问题以添加信息。我现在将设备名称更改为机器,但出现以下错误:Uncaught TypeError: this.props.machines.map is not a function
标签: reactjs react-redux