why react component bind this in constructor

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

In many react compon本文版权归作者所有,未经授权不得转载。著作权归作者所有,禁止商业用途转载。ent’s construc【未经授权禁止转载】著作权归作者所有,禁止商业用途转载。tor, developers alwa【访问 www.tangshuang.net 获取更多精彩内容】【转载请注明来源】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【未经授权禁止转载】【未经授权禁止转载】s regulated by react【转载请注明来源】【本文受版权保护】?

原创内容,盗版必究。【关注微信公众号:wwwtangshuangnet】著作权归作者所有,禁止商业用途转载。

No!

原创内容,盗版必究。【原创不易,请尊重版权】本文作者:唐霜,转载请注明出处。未经授权,禁止复制转载。

In fact, react compo【原创不易,请尊重版权】原创内容,盗版必究。nent class follow ES【关注微信公众号:wwwtangshuangnet】【版权所有,侵权必究】6 class std. The rea著作权归作者所有,禁止商业用途转载。【版权所有】唐霜 www.tangshuang.netson to put this sent【本文受版权保护】未经授权,禁止复制转载。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【关注微信公众号:wwwtangshuangnet】本文作者:唐霜,转载请注明出处。 not point to instan【关注微信公众号:wwwtangshuangnet】本文作者:唐霜,转载请注明出处。ce of this component【作者:唐霜】【本文首发于唐霜的博客】. Let’s look i【访问 www.tangshuang.net 获取更多精彩内容】著作权归作者所有,禁止商业用途转载。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 【本文首发于唐霜的博客】本文作者:唐霜,转载请注明出处。callback function, t原创内容,盗版必究。【未经授权禁止转载】his in the function 【版权所有】唐霜 www.tangshuang.net本文版权归作者所有,未经授权不得转载。does not referer to 【未经授权禁止转载】本文版权归作者所有,未经授权不得转载。the instance of comp著作权归作者所有,禁止商业用途转载。【原创不易,请尊重版权】onent. So another wa【本文受版权保护】【作者:唐霜】y to solve this prob【访问 www.tangshuang.net 获取更多精彩内容】【本文首发于唐霜的博客】lem is to bind this 转载请注明出处:www.tangshuang.net【本文受版权保护】when you use the met本文版权归作者所有,未经授权不得转载。【作者:唐霜】hod, like:

【未经授权禁止转载】转载请注明出处:www.tangshuang.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【关注微信公众号:wwwtangshuangnet】【访问 www.tangshuang.net 获取更多精彩内容】 in say as want you 原创内容,盗版必究。【版权所有】唐霜 www.tangshuang.netwant.

【访问 www.tangshuang.net 获取更多精彩内容】【关注微信公众号:wwwtangshuangnet】本文作者:唐霜,转载请注明出处。