【问题标题】:ReactJs cannot read property 'map' of undefined despite the initial state is set?尽管设置了初始状态,ReactJs 无法读取未定义的属性“地图”?
【发布时间】: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


【解决方案1】:

您将减速器“机器”映射到称为设备的道具。你需要这样做.props.equipment.machines.map

【讨论】:

    【解决方案2】:

    如果您不选择特定的、好的状态,命名 Redux 状态可能会令人困惑 :) 你有一个全局状态是state。你有一个 reducer 并持有一个对象状态,它有一个名为 machines 的属性。

    您再次使用combineReducers 函数作为machines 打开此状态。现在你有了类似的东西。

    • state: 全局状态
    • state.machines: 机器状态在你的全局状态,你正在用你的 reducer 初始化。
    • state.machines.machines:这是保存数组的对象属性。

    这令人困惑 :) 但我相信这还不够令人困惑,因为您在第一个版本中使用了 mapStateToProps 函数:

    return {
        equipment: state.machines,
    }
    

    你在这里做什么?您将state.machines 状态作为equipment 向您的组件打开。然后,如果您想在此处访问您的machines 属性,您应该使用this.props.equipment.machines。因此,如果您使用@borube 的建议,您将解决您的问题。这就是为什么我赞成他/她的回答,但我想确定并要求您提供其他配置。

    现在,你像这样更改了mapStateToProps:

    return {
      machines: state.machines,
    }
    

    您现在拥有的是:state.machines.machines。你应该使用this.props.machines.machines。

    同样,选择更好的名称可能很有用:)

    cmets 后更新

    是的,这很有趣。前一阵子在学习 Redux 时犯了同样的错误,我也自嘲了:)state.someState.someState.foo.bar天哪!然后我试着想出更好的名字。

    这完全取决于您。你的减速器定义是什么?想一个好的通用名称(当然是这个 reducer 状态的通用名称),然后在这个状态下使用属性。

    我不知道你在这里的最终愿望,但你用过一次equipment,那么也许州名可以是equipments,这是复数吗?那你以后会有这样的状态和reducer吗?

    const initialState = {
      machines: [
        {
        id: "1",
        identifier: "My Machine 1",
        location: "My Location 1"
        }
      ],
      cars: [],
      otherEquipments: [],
    };
    
    
    export default function equipments(state=initialState, action) {
      switch (action.type) {
        default:
          return state;
      }
    }
    

    现在,当您使用此状态时,您将到达每个设备:this.props.equipments.machines、this.props.equipments.cars 等。

    为了使用state.machines,你的状态应该是这样的:

    const initialState = [
        {
            id: "1",
            identifier: "My Machine 1",
            location: "My Location 1"
        }
    ];
    

    您将以this.props.machines[0] 的形式访问此项目,或者通过映射来获取此数组的所有项目。如果您将只保留机器,那么您可能会采用这种方式。但是,如果还有其他的设备,把它做成一个物体可能会更好。

    【讨论】:

    • 你让我想笑,同时躲在桌子底下:) 谢谢你的解释。你能指出我选择更好的名字的正确方向吗?我怎样才能避免 state.machines.machines 而只有 state.machines?
    • 不客气。我已经更新了我的答案。我们都会不时自嘲:)
    • 知道了!我不能感谢你。如果可能的话,我会双重接受你的回答:)
    【解决方案3】:

    在mapStateToProps中将设备名称改为“machines”

    【讨论】:

    • 完成。我现在收到错误:未捕获的 TypeError:this.props.machines.map 不是函数。有什么线索吗?谢谢。
    猜你喜欢
    • 2021-11-18
    • 2017-07-30
    • 2020-09-14
    • 2019-10-08
    • 2021-11-17
    • 2018-02-08
    • 2020-04-20
    • 2020-11-18
    • 2021-07-11
    相关资源
    最近更新 更多