简体   繁体   English

在课堂上绑定构造函数或胖箭头

[英]Bind in constructor or fat arrow in class

So i'm wondering if there is a difference between this: 所以我想知道这是否有区别:

import React, { Component, PropTypes } from 'react';

class Example extends Component {
  constructor(props) {
    super(props);
    this.state = {
      page : 1
    };
  }

  nextPage = () => {
    this.setState({ page: this.state.page + 1 });
  }

  previousPage= () => {
    this.setState({ page: this.state.page - 1 });
  }

  render() {
    const { page } = this.state;
    return (
      <div>
        <H1>{page}</H1>
        <Button onClickPrevious={this.previousPage} onClickNext={this.nextPage} />}
      </div>
    );
  }
}

Or 要么

import React, { Component, PropTypes } from 'react';

class Example extends Component {   
    constructor(props) {
         super(props);
         this.nextPage = this.nextPage.bind(this);
         this.previousPage = this.previousPage.bind(this);
         this.state = {
              page: 1
             };
  }

  nextPage() {
    this.setState({ page: this.state.page + 1 });   }

  previousPage() {
    this.setState({ page: this.state.page - 1 });   }

  render() {
    const { page } = this.state;
    return (
      <div>
        <H1>{page}</H1>
        <Button onClickPrevious={this.previousPage} onClickNext={this.nextPage} />}
      </div>
    );   
  }
}

I'm wondering if it's the same in performance this to every function or are there any other benefits? 我想知道每个功能的性能是否相同,还是有任何其他好处?

a bit of further reading( https://medium.com/@esamatti/react-js-pure-render-performance-anti-pattern-fb88c101332f#.khf30fuaq ) 进一步阅读( https://medium.com/@esamatti/react-js-pure-render-performance-anti-pattern-fb88c101332f#.khf30fuaq

The best place to bind your event handlers is your constructor . 绑定事件处理程序的最佳位置是constructor This way your event handler has its context bound to the component instance.You can access props and state and call setState or forceUpdate from such bound handler. 这样,您的事件处理程序将其上下文绑定到组件实例。您可以访问props and state并从此绑定处理程序中调用setStateforceUpdate

Binding with arrow functions have their own advantages as well. arrow功能绑定也有其自身的优点。 Arrow functions always gets the context from where they have been defined. 箭头函数始终从定义它们的位置获取上下文。 So in fact, this example is equivalent to: 所以实际上,这个例子相当于:

The arrow function syntax is a way of defining function with a syntax like this: 箭头函数语法是一种使用如下语法定义函数的方法:

change = (ev) => this.setState({ text: ev.target.value });

It is a more concise way than writing a function(ev) { .... } statement. 它比编写function(ev) { .... }语句更简洁。 If you don't provide { and } brackets after the => arrow, such function is a single expression which is returned instantly. 如果在=>箭头后没有提供{}括号,则此函数是一个立即返回的表达式。 So this desugars to something like: 所以这就像以下一样:

change = function(ev) { return this.setState({ text: ev.target.value }); }.bind(this);

And hence both of .bind() and arrow function cause a new function to be created 因此.bind()arrow函数都会导致创建一个新函数

Concluding, the way you want to bind your function depends on your use case. 总而言之,您希望绑定函数的方式取决于您的用例。

For more details you can read up this article: 欲了解更多详细信息,你可以阅读了文章:

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

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