JavaScript React Basics Understanding State Remove Items From State

Jay McCormick
Jay McCormick
6,646 Points

Anonymous Function in OnClick

Why did we have to use an anonymous function in the button's onClick attribute to call handleRemovePlayer? Why could we not call it directly, as

<button onClick={handleRemovePlayer}>x</button>


If you do that, then the method gets invoked immediately when refreshing your react app. That means that there will be no players left because the method already deleted every player.

When you use () => {} then it only gets invoked when clicked on the button.