I'm trying to call a function while passing the index in. However, I read that it's not best practice to pass a lambda into the onclick handler. Is there a better way of handling this? Because right now isn't a function being created everytime?
Removing the lambda I run the risk of updating the state during a transition which causes infinite calls to the function.
class MyComponent extends React.Component {
constructor(props, context) {
super(props, context);
this.myFunc= this.myFunc.bind(this);
}
myFunc(index){
//do stuff with the index
}
render(){
const items = this.props.list.map((listItem, index) =>
return <div key={listItem.id} onClick={() => this.myFunc(index)}>{listItem.name}</div>
);
return(
<div>{items}</div>
)
}
}