Question about React book. Can I remove li element by another way?

Hello!
I reading the React book from Kirupa. And now I’m in section 15, about TodoList app. According to the book, Event onClick is used for the li element to remove list items:

createTasks(item) {
return <li onClick={() => this.delete(item.key)}
key={item.key}>{item.text}</li>
} 

Next in the book, the delete event handler is defined:

delete(key) {
this.props.delete(key);
}

Out of inexperience, I thought that there is extra code here :slight_smile: I tried to do without the Delete handler. To do this, I specified the following code in the onClick handler of the li element:

createTasks(item) {
        return <li onClick={() => this.props.delete(item.key)}
        key={item.key}>{item.text}</li>
 }

And it works. Please tell me what problems there might be?

There isn’t any problem here :slight_smile:

You condensed your code by removing an extra method definition, which is a good thing. I like having the delete method, for it makes the code more readable. That is also a personal preference.

What we are now discussing is part of the long-running discussions many teams have around whether we should have more code and increase readability…or favor less code and increase complexity haha :robot:

1 Like