Was following this tutorial to make a simple todo app (https://hackernoon.com/create-a-simple-todo-app-in-react-9bd29054566b).
Thing is I've encountered this error:
TypeError: _this is undefined
Referencing to when I delete using onClick in the following component:
import React, {Component} from 'react'
class TodoItems extends Component {
createTasks(item) {
return (
<li key={item.key} onClick={() => this.props.deleteItem(item.key)}>
{item.text}
</li>
)
}
render() {
const todoEntries = this.props.entries
const listItems = todoEntries.map(this.createTasks)
return <ul className="theList">{listItems}</ul>
}
}
export default TodoItems
I have seen that I might have to use .bind(this) in the constructor but I'm not really sure how to apply this here.
Here's is the segment of App.js where deleteItem is defined and TodoItems is called:
deleteItem = key => {
const filteredItems = this.state.items.filter(item => {
return item.key !== key
})
this.setState({
items: filteredItems,
})
}
inputElement = React.createRef()
render() {
return (
<div className="App">
<TodoList
addItem={this.addItem}
inputElement={this.inputElement}
handleInput={this.handleInput}
currentItem={this.state.currentItem}
/>
<TodoItems entries={this.state.items} deleteItem={this.deleteItem}/>
</div> //App
)
}
Thanks in advance for any help!