代码之家  ›  专栏  ›  技术社区  ›  Giulia

组件和减速机中的ReactJs未定义错误

  •  0
  • Giulia  · 技术社区  · 6 年前

    我的ReactJs应用程序中有一个“no unde”问题,控制台说:

    • 未捕获参考错误:设备行未定义设备 18号线和21号线。

    这是我写的代码:

    组件(…组件/设备.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);
    

    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 回复  |  直到 6 年前
        1
  •  1
  •   hawk    6 年前

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

    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>
    
        2
  •  0
  •   Singh    6 年前

    equipment 具有 note

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