简体   繁体   English

在ES6类方法和非React中使用胖箭头语法与否之间有功能上的区别吗?

[英]Are there any functional differences between using fat arrow syntax or not for ES6 class methods and React?

Here are two examples of writing an ES6 class method: 这是编写ES6类方法的两个示例:

The first uses non-fat arrow, or concise method syntax--whatever it is correctly called: 第一种使用非胖箭头或简明的方法语法-无论正确调用什么:

class Test extends Component {
  handleClick() {
    return 'clicked'
  }
}

The second uses fat arrow syntax: 第二种使用胖箭头语法:

class Test2 extends Component {
  handleClick = () => {
    return 'clicked'
  }
}

Should I always prefer to write using fat arrow syntax? 我是否应该始终喜欢使用粗箭头语法编写?

One thing I notice is you could use implicit return which could shorten code. 我注意到的一件事是您可以使用隐式返回,这可以缩短代码。

In most examples I see of people writing constructor() and render() methods in React , they do not use fat arrow syntax for those, but they do for all other class methods. 在大多数示例中,我看到人们在React中编写constructor()render()方法,但它们并未使用胖箭头语法,但所有其他类方法都使用了它们。

Why is this? 为什么是这样?

I am guessing the answer will have something to do with the this keyword since it is fairly intrinsic to classes and the context-preserving nature of fat arrows, but how does this relate to React and fat arrow syntax on class methods? 我猜答案与this关键字有关,因为它是类的固有属性,并且是胖箭头的上下文保留性质,但是这与类方法上的React和胖箭头语法有什么关系? The only case I can think of is that you might be able to avoid binding in the constructor for some cases depending how the class method is called later. 我能想到的唯一情况是,在某些情况下,您可以避免在构造函数中进行绑定,具体取决于以后如何调用类方法。

I would appreciate a scientific answer or a list of cases where a distinction is important. 我希望您能提供科学的答案或区分重要的案例清单。

Consider the render function below 考虑下面的渲染功能

render() {
    return <div onClick={this.handleClick} />
}

When handleClick is defined as an arrow function, the function is executed when the click event is fired. 如果将handleClick定义为箭头函数,则在触发click事件时将执行该函数。 Otherwise it's run at the same time as render . 否则,它将与render同时运行。

Should I always prefer to write using fat arrow syntax? 我是否应该始终喜欢使用粗箭头语法编写?

It depends on how/from where you need to call your function. 这取决于需要/从何处调用函数。 For event handlers, arrow functions are convenient because as you state the component's this is accessible and you also need to pass in the function as opposed to its output. 对于事件处理程序,箭头功能是方便,因为你说出组件的this是访问,你还需要在函数传递,而不是它的输出。

If you don't need to access the component's this or pass your function, then an arrow function is not necessary. 如果您不需要访问组件的this或通过函数,则不需要箭头功能。

Fat arrow functions are context agnostic. 粗箭头功能与上下文无关。 This means that places that normally put function out of context will stay in the proper context. 这意味着通常会将功能置于上下文之外的位置将停留在适当的上下文中。 The most common (possibly only?) use case of this in React is for event handlers. 在React中最常见的(可能是?)用例是事件处理程序。

These are all functionally equivalent. 这些在功能上都是等效的。 All commonplaces: 所有普通的地方:

// 1
handleClick() {
  this.setState({clicked: true})
}

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

//2
constructor() {
  super()
  this.handleClick = this.handleClick.bind(this)
}
handleClick() {
  this.setState({clicked: true})
}

<button onClick={this.handleClick}/>

//3
handleClick() {
  this.setState({clicked: true})
}

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

//4
handleClick = () => {
  this.setState({clicked: true})
}

<button onClick={this.handleClick}/>

Boils down to preference. 归结为偏好。 The only one that you could be 'pegged' for is <button onClick={this.handleClick.bind(this)}/> , for performance issues. 您唯一可以“挂钩”的是<button onClick={this.handleClick.bind(this)}/> ,以解决性能问题。

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

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