简体   繁体   English

在React中编写事件处理程序的最佳方法是什么?

[英]What is the best way to write event handler in React?

I had read the article about handle events in react with arrow functions . 我读过有关处理事件的文章与箭头函数作出反应 And the final way in that article is not likely the best because of re-render issue. 由于重新渲染问题,该文章的最后一种方式可能不是最好的。

eg 例如

 class Example extends React.Component { handleSelect = i => e => { console.log(i) } render() { return {this.state.language.map(item, i) => ( <ListItem ... onPress={this.handleSelect(i)} // re-render sub component every time the Example render() because of the annoymous function /> )} } } 

I wonder which way is the best to write event handler in React? 我想知道哪种方式最好在React中编写事件处理程序?

In order to get the best performance out of React, you need to minimize the number of objects that are being created during renders. 为了从React中获得最佳性能,您需要最小化渲染期间创建的对象数。 And as a reminder a function declaration (eg function myFunc or const func = () => {} ) creates an object. 作为提醒,函数声明(例如function myFuncconst func = () => {} )创建一个对象。


I would say your code has an unsolvable issue because you're creating a new instance of the inner function when handleSelect is invoked: 我会说你的代码有一个无法解决的问题,因为你在调用handleSelect时创建了一个新的内部函数实例:

handleSelect = i => e => {
  console.log(i)
}

I'll rewrite this using the function notation because it's a bit more clear what happening (but I prefer arrow functions in practice): 我将使用function表示法重写它,因为它更清楚发生了什么(但我更喜欢实际中的箭头函数):

handleSelect = function (i) {
  return function (e) {
    console.log(i);
  }
}

The issue here is the with each render when you invoke handleSelect it creates a brand new inner function (ie function (e) {/* ... */} ). 这里的问题是当你调用handleSelect每个渲染 它创建一个全新的内部函数 (即function (e) {/* ... */} )。


I mentioned that your code has an unsolvable issue because there is no way to split up your curried handleSelect function because you're passing the index i that's created inside the render function . 我提到你的代码有一个无法解决的问题,因为你没有办法拆分你的curried handleSelect函数,因为你传递了在render函数中创建的索引i Because that state doesn't exist anywhere else, you have to create a new function to close-over it every time and that's okay. 因为该状态在其他任何地方都不存在,所以你必须创建一个新函数来每次都关闭它,这没关系。

I would even refactor your code like so: 我甚至会像这样重构你的代码:

class Example extends React.Component {
  // as @RickJolly mentioned, this method doesn't have to be arrow
  handleSelect (i) {
    console.log(i)
  }

  render() {
    return {this.state.language.map(item, i) => (
      <ListItem 
        ...
        onPress={() => this.handleSelect(i)}
    )}

  }
}

If you have to create a new function every time, then you might as well inline it vs returning a function. 如果你需要创建一个新的功能,每一次,那么你还不如内联VS它返回的功能。 That's preference though. 但这是首选。


Edit 编辑

As @RickJolly mentioned, if your method doesn't use this then it shouldn't be an arrow function. 正如@RickJolly所提到的,如果你的方法不使用this那么它不应该是一个箭头函数。

From his comment: 从他的评论:

since you're calling () => this.handleSelect(i) via an arrow function, this is bound to the this of the enclosing context [which is the class pointer] 因为你打电话() => this.handleSelect(i)通过箭头功能, this势必会在this封闭的上下文[其为类指针]的

暂无
暂无

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

相关问题 编写此 React 代码的最佳方法是什么? - What is the best way to write this React code? 为了不在javascript中杀死事件处理程序,有什么方法? - What is the way in order not to kill event handler in javascript? 从React中的事件处理程序中删除动态DOM节点的正确方法是什么? - what's the right way to remove dynamic DOM Nodes from an event handler in React? 用于捕获和存储在 React Js 应用程序中输入的数据的最佳事件处理程序 - Best Event Handler for capturing and storing the data entered in React Js Application 如何使用“this”在react组件中编写内联js事件处理程序代码 - how to write inline js event handler code in the react component using “this” Type Script - React Native:在 ternary 中编写 ternary 的最佳实践方式是什么? - Type Script - React Native : What is the best practice way to write ternary inside ternary? 有没有一种方法可以在JavaScript中编写事件处理程序,而无需将其添加到标记中或使用addEvent? - Is there a way to write an event handler in JavaScript without adding it to the tag or using addEvent? 在页面中编写for in循环的最佳方法是什么 - what is the best way to write a for in loop to the page 用javascript编写正则表达式的最佳方法是什么? - What is the best way to write a regular expression in javascript? 用JavaScript编写对象方法的最佳方法是什么? - What is the best way to write object methods in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM