In my react component where I try to render a list based on material-ui List component, I have the following problem.
I loop through an array of contact objects to render a list of ListItem, but I want to be able to handle click event on each List item.
I use the onTouchTap prop and tries to call my onSelectedContact method but how do I pass in the current contact object from the map function?
{this.props.contacts.map(( contact, i ) => <span key={i}>
<ListItem
key={i}
primaryText={contact.name}
onTouchTap={this.onSelectContact?????}
/>
}
this.onSelectContact.bind(this, contact)
– MaxxonTouchTap={onSelectContact()}
). You execute function and pass its result. Right isonTouchTap={onSelectContact}
you pass just handler. WhenListItem
doesthis.props.onTouchTap()
its changes its context, it means you can't (for example) dothis.setState()
in parent component handler because context in handler is ListItem's props object. Sry for my english ) – Maxx