I am mapping over an array of objects that looks like this
events = [
{
id: 1,
name: 'Real vs Barcelona'
},
{
id: 2,
name: 'Celtic vs Rangers'
}
];
I then want to pass the id to a function that updates the relevant event. This work, I am using an arrow function to achieve this.
onClick={() => this.props.updateEvent(event.id)}
render() {
return (
<div>
{this.props.events.map(event => {
return <p key={event.id} onClick={() => this.props.updateEvent(event.id)}>{ event.name }</p>
});
</div>
)
}
I have read there are performance implications of doing it this way, and also my es-linter is showing an error. Does anyone know another way I can approach this and pass the id in a different way.I don't think I want to use the ES5 bind
approach either.
Maybe something like this
render() {
let updateEvent = (e) => {
//This obviously won't work but something similar?
this.props.updateEvent(e.target);
}
return (
<div>
{this.props.events.map(event => {
return <p key={event.id} onClick={updateEvent}>{ event.name }</p>
});
</div>
)
}