I was trying to pass both the event and some other data (the index of an element) to an event handler in a React App. I found here that I should make a higher-order
function, but I don't get a pleasant result. Here is a snippet of my code:
const changeNameHandler = (index) => {
return (event) => {
console.log(index, event);
};
};
let persons = null;
if(showValuesState) {
persons = (
<div>
{
personsState.map((person, index) => {
return <Person
name = {person.name}
age = {person.age}
click = {() => deletePersonHandler(index)}
key = {index}
changeName = {() => changeNameHandler(index)()}/>
})
}
</div>
);
}
I am trying to pass the index to changeNameHandler
function while also getting the event, but the value of the event variable from that console.log(index, event)
statement is still undefined
.