why react component bind this in constructor

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

In many react compon【原创不易,请尊重版权】【版权所有,侵权必究】ent’s construc本文版权归作者所有,未经授权不得转载。本文版权归作者所有,未经授权不得转载。tor, developers alwa本文版权归作者所有,未经授权不得转载。【本文首发于唐霜的博客】ys bind its methods 【转载请注明来源】【转载请注明来源】with this, like:

著作权归作者所有,禁止商业用途转载。【版权所有,侵权必究】原创内容,盗版必究。【原创不易,请尊重版权】
export default class MyComponent extends Component {
  constructor() {
    this.say = this.say.bind(this)
  }
  ...
}

This sentence seems 【本文受版权保护】【未经授权禁止转载】to be stupid. Is thi【原创内容,转载请注明出处】未经授权,禁止复制转载。s regulated by react未经授权,禁止复制转载。【版权所有】唐霜 www.tangshuang.net?

本文版权归作者所有,未经授权不得转载。【未经授权禁止转载】【本文首发于唐霜的博客】【本文首发于唐霜的博客】

No!

【版权所有,侵权必究】【访问 www.tangshuang.n著作权归作者所有,禁止商业用途转载。【未经授权禁止转载】et 获取更多精彩内容】【本文受版权保护】

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

原创内容,盗版必究。【本文首发于唐霜的博客】【版权所有】唐霜 www.tangshu【版权所有】唐霜 www.tangshuang.net【原创不易,请尊重版权】ang.net【关注微信公众号:wwwtangshua【转载请注明来源】【版权所有】唐霜 www.tangshuang.netngnet】
<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本文版权归作者所有,未经授权不得转载。callback function, t【作者:唐霜】【原创不易,请尊重版权】his in the function 【原创不易,请尊重版权】转载请注明出处:www.tangshuang.netdoes not referer to 转载请注明出处:www.tangshuang.net【作者:唐霜】the instance of comp【作者:唐霜】本文版权归作者所有,未经授权不得转载。onent. So another wa【作者:唐霜】【版权所有】唐霜 www.tangshuang.nety to solve this prob原创内容,盗版必究。【版权所有】唐霜 www.tangshuang.netlem is to bind this 【原创不易,请尊重版权】【版权所有,侵权必究】when you use the met【版权所有】唐霜 www.tangshuang.net本文作者:唐霜,转载请注明出处。hod, like:

未经授权,禁止复制转载。【关注微信公众号:wwwtangshua著作权归作者所有,禁止商业用途转载。【原创内容,转载请注明出处】ngnet】
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.net【原创内容,转载请注明出处】want.

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