admin管理员组文章数量:1410724
I am trying to create a listitem click listener with the item as argument in React. But the function itemClick is now called when page is loaded in stead of when I click on a item.
itemClick = (item) => {
console.log(item)
}
render(){
return(
<Fragment>
{this.state.list.map(item=> (
<tr onClick={this.itemClick(item)}>
<td>{item.firstname}</td>
<td>{item.lastname}</td>
</tr>
))}
</Fragment>
)
}
when I do this the function will be called when i clicked but I can't get the argument
<tr onClick={this.itemClick} >
Help?
I am trying to create a listitem click listener with the item as argument in React. But the function itemClick is now called when page is loaded in stead of when I click on a item.
itemClick = (item) => {
console.log(item)
}
render(){
return(
<Fragment>
{this.state.list.map(item=> (
<tr onClick={this.itemClick(item)}>
<td>{item.firstname}</td>
<td>{item.lastname}</td>
</tr>
))}
</Fragment>
)
}
when I do this the function will be called when i clicked but I can't get the argument
<tr onClick={this.itemClick} >
Help?
Share Improve this question asked Nov 13, 2018 at 10:32 user3432681user3432681 6644 gold badges12 silver badges27 bronze badges 2-
onClick={(e) => this.itemClick(item, e)}
oronClick={this.itemClick.bind(this, item)}
– Mayank Shukla Commented Nov 13, 2018 at 10:34 - This has to do with the binding on "this". See Handling Events on React for further information about event handling and this for further information about binding in functions. – Agash Thamo. Commented Nov 13, 2018 at 10:36
1 Answer
Reset to default 7By writing this.itemClick(item)
you are invoking itemClick
directly on render. You can instead create a new inlined function that calls itemClick
when it is invoked.
<tr onClick={() => this.itemClick(item)}>
本文标签: javascripthow to call function with parameter in JSXStack Overflow
版权声明:本文标题:javascript - how to call function with parameter in JSX? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744816728a2626741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论