0

I was goin through some tutorial, and i got into an issue with The error 'cannot read property 'map' of undefined'.Please help me with the following code.

TypeError: Cannot read property 'map' of undefined 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;
2
  • do var todoEntries = this.props.entries || []; you don't have items in state in your TodoList component. Commented May 26, 2019 at 21:05
  • the problem is solved but it is not taking the entries and displaying them. Commented May 26, 2019 at 21:15

1 Answer 1

1

You're trying to pass this.state.items as entries, but it seems that it's not an array but undefined.

You could just make it an array by default by doing something like the following -

class MyComponent extends Component {
  state = {
    items: []
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

class TodoList extends Component { constructor(props){ super(props); this.state = { item: [] }; this.additem = this.additem.bind(this); } this is my code and yes i made sure it is an array
@Aishwarya item is not items
@Aishwarya you wrote item while the correct word according to your code is items. Try fixing that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.