why react component bind this in constructor

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

In many react compon【作者:唐霜】【作者:唐霜】ent’s construc本文版权归作者所有,未经授权不得转载。【原创内容,转载请注明出处】tor, developers alwa【本文首发于唐霜的博客】【作者:唐霜】ys bind its methods 【原创不易,请尊重版权】转载请注明出处:www.tangshuang.netwith this, like:

【作者:唐霜】【版权所有,侵权必究】本文版权归作者所有,未经授权不得转载。
export default class MyComponent extends Component {
  constructor() {
    this.say = this.say.bind(this)
  }
  ...
}

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

【版权所有】唐霜 www.tangshu【本文首发于唐霜的博客】【原创内容,转载请注明出处】ang.net【原创不易,请尊重版权】本文版权归作者所有,未经授权不得转载。【原创内容,转载请注明出处】

No!

【转载请注明来源】本文作者:唐霜,转载请注明出处。转载请注明出处:www.tangshua【关注微信公众号:wwwtangshuangnet】本文作者:唐霜,转载请注明出处。ng.net

In fact, react compo未经授权,禁止复制转载。原创内容,盗版必究。nent class follow ES【原创内容,转载请注明出处】本文版权归作者所有,未经授权不得转载。6 class std. The rea原创内容,盗版必究。本文版权归作者所有,未经授权不得转载。son to put this sent转载请注明出处:www.tangshuang.net【未经授权禁止转载】ence is because when【未经授权禁止转载】著作权归作者所有,禁止商业用途转载。 you use this.say in【原创不易,请尊重版权】【转载请注明来源】 your render, you al【未经授权禁止转载】本文作者:唐霜,转载请注明出处。way use it as onClic转载请注明出处:www.tangshuang.net【原创内容,转载请注明出处】k callback function,原创内容,盗版必究。未经授权,禁止复制转载。 this is why this is本文作者:唐霜,转载请注明出处。原创内容,盗版必究。 not point to instan【作者:唐霜】本文版权归作者所有,未经授权不得转载。ce of this component【版权所有,侵权必究】【关注微信公众号:wwwtangshuangnet】. Let’s look i本文版权归作者所有,未经授权不得转载。【原创不易,请尊重版权】nto this code:

【未经授权禁止转载】未经授权,禁止复制转载。未经授权,禁止复制转载。【本文首发于唐霜的博客】
<a href="" onclick="alert(this.href)">click</a>

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

转载请注明出处:www.tangshua【版权所有,侵权必究】【未经授权禁止转载】ng.net本文版权归作者所有,未经授权不得转载。
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【本文首发于唐霜的博客】【版权所有】唐霜 www.tangshuang.net in say as want you 【关注微信公众号:wwwtangshuangnet】转载请注明出处:www.tangshuang.netwant.

未经授权,禁止复制转载。转载请注明出处:www.tangshua【版权所有】唐霜 www.tangshuang.net【作者:唐霜】ng.net原创内容,盗版必究。【版权所有】唐霜 www.tangshu本文版权归作者所有,未经授权不得转载。本文版权归作者所有,未经授权不得转载。ang.net