[英]React proper way to pass function as prop with parameters
If I try to pass a function with parameters as props from a Parent
component to a Child
component in this way: 如果我尝试以这种方式将带有参数的函数从
Parent
组件传递给Child
组件:
handleClick={() => handleClick(tiposObjetivos.RENTA_TEMPORAL)}
The Child component will re-render everytime Parent is re-render as it creates the function in every render. 每当Parent重新渲染时,Child组件将重新渲染,因为它在每个渲染中创建了该函数。 What is the better way to pass a function as a prop with parameters?
将函数作为带参数的prop传递的更好方法是什么?
there are two ways to pass function as props 有两种方法可以将函数作为道具传递
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
The above two lines are equivalent, and use arrow functions and Function.prototype.bind respectively. 以上两行是等效的,分别使用箭头函数和Function.prototype.bind。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.