【问题标题】:Problem with: TypeError: Cannot read property 'map' of undefined问题:TypeError:无法读取未定义的属性“地图”
【发布时间】:2019-05-27 05:08:03
【问题描述】:

我正在阅读一些教程,但遇到了错误“无法读取未定义的属性 'map'”的问题。请帮助我使用以下代码。

TypeError:无法读取未定义的属性“地图” TodoItems.render C:/Users/hp/Desktop/todo_list/src/TodoItems.js:10

 7 | 
   8 |    render(){
   9 |        var todoEntries = this.props.entries;
> 10 |        var listItems = todoEntries.map(this.createTasks);
     | ^  11 | 
  12 |        return (
  13 |            <ul className="theList">
View compiled
▶ 23 stack frames were collapsed.

import React, { Component } from "react";

class TodoItems extends Component{
    createTasks(item) {
        return <li key={item.key}>{item.text}</li>
    }

    render(){
        var todoEntries = this.props.entries;
        var listItems = todoEntries.map(this.createTasks);

        return (
            <ul className="theList">
                {listItems}
            </ul>
        );
    }
}

export default TodoItems;


and the TodoList file code is:
render() {
        return (
            <div className="todoListMain">
            <div className="header">
                <form onSubmit={this.addItem}>
                    <input ref={(a) => this._inputElement = a}
                         placeholder="enter task">
                    </input>
                    <button type="submit">add</button>
                </form>
            </div>
            <TodoItems entries={this.state.items}/>
        </div>
        );
    }
}

export default TodoList;

【问题讨论】:

  • var todoEntries = this.props.entries || []; 你的TodoList 组件中没有处于状态的项目。
  • 问题已解决,但没有获取条目并显示它们。

标签: node.js reactjs


【解决方案1】:

您试图将this.state.items 传递为entries,但它似乎不是数组而是undefined

您可以通过执行以下操作将其设为默认数组 -

class MyComponent extends Component {
  state = {
    items: []
  }
}

【讨论】:

  • 类 TodoList 扩展 Component { constructor(props){ super(props); this.state = { 项目:[] }; this.additem = this.additem.bind(this);这是我的代码,是的,我确定它是一个数组
  • @Aishwarya item 不是 items
  • @Aishwarya 您写了item,而根据您的代码,正确的单词是items。尝试修复它
猜你喜欢
  • 1970-01-01
  • 2022-01-14
  • 2022-01-01
  • 2019-03-02
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
相关资源
最近更新 更多