[英]Best practice in React regarding the context `this`
Which of the following is the best practice with regard to performance in a React class component: 以下哪项是关于React类组件中的性能的最佳实践:
Binding a call back function in the constructor: 绑定构造函数中的回调函数:
constructor(props) { /* some code */ this.onChange= this.onChange.bind(this) } render() { return( <div> <input onChange={this.onChange} </div> ); } onChange(event) { /* some code involving 'this' */ }
Using an arrow function instead of a normal function: 使用箭头函数而不是正常函数:
constructor(props) { /* some code */ } render() { return( <div> <input onChange={this.onChange} </div> ); } onChange = (event) => { /* some code involving 'this' */ }
Sticking to a normal function but declaring an arrow function in the onChange field: 坚持正常功能但在onChange字段中声明箭头功能:
constructor(props) { /* some code */ } render() { <div> <input onChange={(event) => {this.onChange(event)}} </div> } onChange(event) { /* some code involving 'this' */ }
Thanks! 谢谢!
All 3 options, with regards to this
, behave the same. this
,所有3个选项的行为都相同。
Option 3 is creating a new function on every render and is thus less desired than options 1 and 2 (since such re-creation is unnecessary and could potentially hinder performance) 选项3在每个渲染上创建一个新函数,因此不如选项1和2(因为这种重新创建是不必要的,可能会阻碍性能)
As far as options 1 and 2, they come down to the same behavior with slight differences unrelated to the behavior of this
. 至于选择1和2,它们归结为与无关的行为的细微差别相同的行为
this
。
The trick to understanding why they behave the same with this
is to know what the following code does: 理解它们为什么与
this
行为相同的技巧是知道以下代码的作用:
method = () => {}
It's just syntactic sugar to add a method to an instance: 将方法添加到实例只是语法糖:
class A {
method = () => {}
}
// is the same as
class A {
constructor() {
this.method = () => {}
}
}
See how Babel transpiles it . 看看巴贝尔如何解释它 。
Since an arrow function inherits the context it is defined in as this
, the context for option 2 is the class itself. 由于箭头函数继承它是在定义的上下文作为
this
,对于选项2的上下文中是类本身。
class A { constructor() { this.method = () => { return this; // ^^^^ this points to the instance of A } } } const a = new A(); console.log(a.method() === a); // true
Which is the same thing for option 1: 选项1的内容相同:
class A { constructor() { this.method = this.method.bind(this); } method() { return this; // ^^^^ this points to the instance of A } } const a = new A(); console.log(a.method() === a); // true
That means your options come down to the difference between: 这意味着你的选择归结为:
// option 1
constructor(props) {
this.onChange = this.onChange.bind(this)
}
onChange() {
// code for onChange...
}
and 和
// option 2
constructor(props) {
this.onChange = () => /* code for onChange */
}
The main advantage I would give to option 1 is that it has a named function instead of an arrow function, which makes debugging a bit easier when examining stack traces (although function name inferences dilutes this point a bit). 我给选项1的主要优点是它有一个命名函数而不是箭头函数,这使得在检查堆栈跟踪时调试更容易一些(尽管函数名称推断会稍微淡化这一点)。
The main advantage I would give to option 2 is that it's a bit "cleaner" looking, as in less verbose, code but that is a subjective opinion. 我给选项2的主要优点是它看起来有点“干净”,就像在较不详细的代码中那样,但这是一种主观意见。
Overall, option 1 and option 2 are practically indifferent. 总的来说,选项1和选项2实际上是无关紧要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.