Component和Reducer中的ReactJs未定义错误

时间:2018-08-17 08:06:36

标签: reactjs react-redux

控制台上显示我的ReactJs应用程序出现“ no-undef”问题:

  • 未捕获的ReferenceError:未在设备行中定义设备 18和21行。
  • 未捕获的ReferenceError:未在Object ../ src / reducers / equipment.js(equipment.js:5)中定义标识符

这是我编写的代码:

组件(..components / Equipment.js):

import React, { Component } from 'react';
import {connect} from 'react-redux';


class Equipment extends Component {
  render() {
    return (
      <div>
        {this.props.equipment.map(note => (
          <div className="card">
            <div className="card-header">
              Equipment
            </div>
            <div className="card-content">
              <table>
                <tbody>
                  <tr>
                    <td>{equipment.identifier}</td>
                  </tr>
                  <tr>
                    <td>{equipment.location}</td>
                  </tr>
                </tbody>
              </table>
              <button>edit</button><button>delete</button>
            </div>
          </div>
        ))}
      </div>
    )
  }
}

const mapStateToProps = state => {
  return {
    equipment: state.equipment,
  }
}

const mapDispatchToProps = dispatch => {
  return {
  }
}


export default connect(mapStateToProps, mapDispatchToProps)(Equipment);

Reducer(..reducers / equipment.js):

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


export default function equipment(state=initialState, action) {
  switch (action.type) {
    default:
      return state;
  }
}

关于错误到底在哪里的任何想法? 谢谢。

2 个答案:

答案 0 :(得分:1)

修复您的初始状态,应该是对象而不是数组:

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

也可以将equipment更改为note

<tr>
   <td>{note.identifier}</td>
 </tr>
 <tr>
   <td>{note.location}</td>
 </tr>

答案 1 :(得分:0)

在Equipment.js中用equipment替换note

<tr>
   <td>{note.identifier}</td>
</tr>
<tr>
   <td>{note.location}</td>
</tr>