why react component bind this in constructor

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

In many react compon【关注微信公众号:wwwtangshuangnet】原创内容,盗版必究。ent’s construc未经授权,禁止复制转载。【作者:唐霜】tor, developers alwa【版权所有,侵权必究】【版权所有】唐霜 www.tangshuang.netys bind its methods 著作权归作者所有,禁止商业用途转载。原创内容,盗版必究。with this, like:

本文作者:唐霜,转载请注明出处。本文版权归作者所有,未经授权不得转载。本文版权归作者所有,未经授权不得转载。【未经授权禁止转载】
export default class MyComponent extends Component {
  constructor() {
    this.say = this.say.bind(this)
  }
  ...
}

This sentence seems 转载请注明出处:www.tangshuang.net本文版权归作者所有,未经授权不得转载。to be stupid. Is thi本文版权归作者所有,未经授权不得转载。【版权所有,侵权必究】s regulated by react【版权所有】唐霜 www.tangshuang.net【原创内容,转载请注明出处】?

【关注微信公众号:wwwtangshuangnet】原创内容,盗版必究。【转载请注明来源】

No!

【本文首发于唐霜的博客】未经授权,禁止复制转载。【关注微信公众号:wwwtangshuangnet】

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

【访问 www.tangshuang.net 获取更多精彩内容】【原创不易,请尊重版权】