问题出现:TypeError:无法读取未定义的属性“ map”

时间:2019-05-26 21:00:42

标签: node.js reactjs

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

TypeError:无法读取未定义的属性“ map” 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;

1 个答案:

答案 0 :(得分:0)

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

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

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