why react component bind this in constructor

广告位招租
扫码页面底部二维码联系

In many react compon【未经授权禁止转载】转载请注明出处:www.tangshuang.netent’s construc【原创内容,转载请注明出处】【转载请注明来源】tor, developers alwa未经授权,禁止复制转载。原创内容,盗版必究。ys bind its methods 【转载请注明来源】本文作者:唐霜,转载请注明出处。with this, like:

著作权归作者所有,禁止商业用途转载。未经授权,禁止复制转载。【版权所有】唐霜 www.tangshuang.net原创内容,盗版必究。
export default class MyComponent extends Component {
  constructor() {
    this.say = this.say.bind(this)
  }
  ...
}

This sentence seems 未经授权,禁止复制转载。【版权所有】唐霜 www.tangshuang.netto be stupid. Is thi转载请注明出处:www.tangshuang.net著作权归作者所有,禁止商业用途转载。s regulated by react未经授权,禁止复制转载。【原创不易,请尊重版权】?

【未经授权禁止转载】本文版权归作者所有,未经授权不得转载。

No!

【未经授权禁止转载】【未经授权禁止转载】

In fact, react compo【原创内容,转载请注明出处】【版权所有,侵权必究】nent class follow ES【本文受版权保护】原创内容,盗版必究。6 class std. The rea【版权所有】唐霜 www.tangshuang.net【本文受版权保护】son to put this sent【未经授权禁止转载】【作者:唐霜】ence is because when转载请注明出处:www.tangshuang.net著作权归作者所有,禁止商业用途转载。 you use this.say in【版权所有,侵权必究】【转载请注明来源】 your render, you al【访问 www.tangshuang.net 获取更多精彩内容】转载请注明出处:www.tangshuang.netway use it as onClic未经授权,禁止复制转载。原创内容,盗版必究。k callback function,未经授权,禁止复制转载。【版权所有,侵权必究】 this is why this is著作权归作者所有,禁止商业用途转载。【本文首发于唐霜的博客】 not point to instan转载请注明出处:www.tangshuang.net【版权所有】唐霜 www.tangshuang.netce of this component【未经授权禁止转载】本文作者:唐霜,转载请注明出处。. Let’s look i【本文受版权保护】著作权归作者所有,禁止商业用途转载。nto this code:

本文版权归作者所有,未经授权不得转载。转载请注明出处:www.tangshuang.net
<a href="" onclick="alert(this.href)">click</a>

You know what this p原创内容,盗版必究。【作者:唐霜】oint to here, so whe原创内容,盗版必究。本文作者:唐霜,转载请注明出处。n you use a componen转载请注明出处:www.tangshuang.net【本文首发于唐霜的博客】t method as a event 【未经授权禁止转载】原创内容,盗版必究。callback function, t【关注微信公众号:wwwtangshuangnet】未经授权,禁止复制转载。his in the function 【版权所有】唐霜 www.tangshuang.net著作权归作者所有,禁止商业用途转载。does not referer to 著作权归作者所有,禁止商业用途转载。原创内容,盗版必究。the instance of comp【原创内容,转载请注明出处】【作者:唐霜】onent. So another wa【版权所有,侵权必究】著作权归作者所有,禁止商业用途转载。y to solve this prob【原创内容,转载请注明出处】本文版权归作者所有,未经授权不得转载。lem is to bind this 【关注微信公众号:wwwtangshuangnet】转载请注明出处:www.tangshuang.netwhen you use the met【作者:唐霜】【原创不易,请尊重版权】hod, like:

【未经授权禁止转载】未经授权,禁止复制转载。【本文首发于唐霜的博客】
export default class MyComponent extends Component {
  constructor() {
    // do not use bind sentence
  }
  render() {
    return <a onClick={this.say.bind(this)}>click</a>
  }
}

Now you can use this原创内容,盗版必究。著作权归作者所有,禁止商业用途转载。 in say as want you 未经授权,禁止复制转载。【版权所有】唐霜 www.tangshuang.netwant.

【关注微信公众号:wwwtangshuangnet】著作权归作者所有,禁止商业用途转载。本文作者:唐霜,转载请注明出处。