rangeme rangeme - 2 months ago 28
Javascript Question

How can I pass variables to function from onClick in Meteor/React?

I have some code like so

goToEvent() {
FlowRouter.go('/event/${id}');
}

return (
{e.users.map( (id) => {
<ListItem key={id} primaryText="Event" onClick={this.goToEvent.bind(this)} />
})}
);


I'm unsure how to get the id parameter into the
goToEvent()
.

I actually have the bind in the constructor so I tried having
onClick={this.goToEvent(id)}
but realized that was incorrect because it was calling the method immediately.

Any help or direction would be appreciated.

Answer

For react the solution is

return ( {e.users.map( (id) => { <ListItem key={id} primaryText="Event" onClick={this.goToEvent.bind(this, id)} /> })} );