React-sortable-hoc: onClick event is triggering on component render

Created on 18 Jul 2017  路  2Comments  路  Source: clauderic/react-sortable-hoc

I am using a delete icon inside list item to give the ability to remove the list item. I am adding onClick method on image icon. But every time component loads onClick method is triggering automatically and removing all the list items. And after component loads onClick event doesn't work. Any pointer what could be the work around for this to stop triggering onClick method on component render. I have already tried pressDelay and distance props but doesn't seems work.

Here is my code reference

handleDelete(index) {
    this.props.removeItem(index)
  }
render() {
  return (
    <ListItem style={styles.item} key={`item-${key}`} index={key} handle>
       { items[key].name}
       <img src="./images/icon-remove.png" width="16" onClick={this.handleDelete(key)} />
     </ListItem>
  )
}

Most helpful comment

Hi @raghav1086,

Your click handler is being called on load because you're calling it on load ;). The contents of the {} are executed on load, so this.handleDelete(key) is literally trying to call your handler as soon as it is seen. To set a click handler _and_ pass it a specific value, you have to make your handler a function that calls your handler, something like

onClick={() => this.handleDelete(key)}

All 2 comments

Hi @raghav1086,

Your click handler is being called on load because you're calling it on load ;). The contents of the {} are executed on load, so this.handleDelete(key) is literally trying to call your handler as soon as it is seen. To set a click handler _and_ pass it a specific value, you have to make your handler a function that calls your handler, something like

onClick={() => this.handleDelete(key)}

@cameronmcefee is absolutely right, thanks for jumping in on this issue 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

silvenon picture silvenon  路  4Comments

ncammarata picture ncammarata  路  4Comments

ianmstew picture ianmstew  路  3Comments

ricokahler picture ricokahler  路  3Comments

sammiwei911 picture sammiwei911  路  3Comments