I have a little understanding question and I will very thankful if you could help to clarify it.
I am building a little “todo list” app to practice React. I could just figure out that in order to delete the item from an array in the state of my main parent “App component” I have to pass the child component’s id, as the argument in another props of the delete handleDelete function like that
I’m not sure what you mean by “empty array function” – the first example is just a function. Second one is a function call, it’s just some value, it’s not a function definition.
Event handlers in JavaScript take the former (a function) as an argument – they need a function that they can run when the event happens.
The second one is whatever the return value of that function is, and as that’s undefined, what’s there is literally the same as writing:
I kind of got it. However, it is still not what i meant. Maybe it would be more clear if I share the whole code.
Here is the parent and child components where I pass the function handleDelete as a props to child.
Right, so when a click event happens you want a function to run that handles the delete. So
() => handleDelete(id)
That’s a function that when it runs, handles delete.
This
handleDelete(id)
Is just whatever the value of that is, I assume undefined. Every time onClick happens, it runs…undefined. Which is not a function, doesn’t do anything useful.
running the function will do something useful, but there it will just be resolved to a value when the code is evaluated (it may actually delete one, but accidentally, not because of a button click)