I have a list containing about 2 thousand items. If I use onClick for each child, I get 2k listeners, which I currently have. I would like to do something like having the parent component listen for click events instead. But if I do this, I have no reference to the child component that I need to call setState. Also, the list of child components can be filtered dynamically (can using this.refs be bad?).
The best I can come up with is to have the hash identification of the id child components by the child components in the parent and look at the click view.
For illustrative purposes only:
var Parent = React.createClass({ shouldComponentUpdate: function() { return false; }, handler: function(e) { // look up from reference and set state }, componentWillUnmount: function() { // clean up reference }, render: function() { this.reference = {}; var items = []; for(var i = 0; i < this.props.items.length; i++) { var child = React.createClass(Child, {id: this.props.items[i].id}); items.push(child); reference[child.id] = child; } return React.createClass('div', {onClick: this.handler}, items); } })
I wonder if there is a way for React to handle this.
source share