简体   繁体   English

React Native Button onPress 函数示例

[英]React Native Button onPress function example

使用两者之间有什么区别,我应该何时使用一种而不是另一种

<Button onPress={this.refreshData}>
<Button onPress={()=>this.refreshData()}>

Here is an extract from React Docs.这是 React Docs 的摘录。

You can use an arrow function to wrap around an event handler and pass parameters:您可以使用箭头函数来环绕事件处理程序并传递参数:

<button onClick={() => this.handleClick(id)} />

This is equivalent to calling .bind:这相当于调用 .bind:

<button onClick={this.handleClick.bind(this, id)} />

https://reactjs.org/docs/faq-functions.html https://reactjs.org/docs/faq-functions.html

You should do this if you don't need to pass an argument to your function :如果您不需要将参数传递给函数,则应该这样做:

<Button onPress={this.refreshData}>

Doing this <Button onPress={()=>this.refreshData()}> may break optimizations.这样做<Button onPress={()=>this.refreshData()}>可能会破坏优化。
From React doc:来自 React 文档:

Using an arrow function in render creates a new function each time the component renders, which may break optimizations based on strict identity comparison.每次组件渲染时,在 render 中使用箭头函数都会创建一个新函数,这可能会破坏基于严格标识比较的优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM