why react component bind this in constructor

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

In many react compon【未经授权禁止转载】【作者:唐霜】ent’s construc【访问 www.tangshuang.net 获取更多精彩内容】【版权所有】唐霜 www.tangshuang.nettor, developers alwa【版权所有】唐霜 www.tangshuang.net转载请注明出处:www.tangshuang.netys bind its methods 【原创内容,转载请注明出处】【访问 www.tangshuang.net 获取更多精彩内容】with this, like:

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

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

【转载请注明来源】【本文首发于唐霜的博客】

No!

本文版权归作者所有,未经授权不得转载。【原创内容,转载请注明出处】原创内容,盗版必究。

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

著作权归作者所有,禁止商业用途转载。著作权归作者所有,禁止商业用途转载。【原创不易,请尊重版权】【版权所有】唐霜 www.tangshuang.net
<a href="" onclick="alert(this.href)">click</a>

You know what this p转载请注明出处:www.tangshuang.net【版权所有】唐霜 www.tangshuang.netoint to here, so whe本文作者:唐霜,转载请注明出处。原创内容,盗版必究。n you use a componen【版权所有,侵权必究】【版权所有】唐霜 www.tangshuang.nett method as a event 本文作者:唐霜,转载请注明出处。本文版权归作者所有,未经授权不得转载。callback function, t【转载请注明来源】未经授权,禁止复制转载。his in the function 【原创不易,请尊重版权】【未经授权禁止转载】does not referer to 未经授权,禁止复制转载。【关注微信公众号:wwwtangshuangnet】the instance of comp【版权所有】唐霜 www.tangshuang.net本文作者:唐霜,转载请注明出处。onent. So another wa【原创内容,转载请注明出处】本文作者:唐霜,转载请注明出处。y to solve this prob【版权所有,侵权必究】【未经授权禁止转载】lem is to bind this 著作权归作者所有,禁止商业用途转载。【原创内容,转载请注明出处】when you use the met本文版权归作者所有,未经授权不得转载。转载请注明出处:www.tangshuang.nethod, 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【版权所有】唐霜 www.tangshuang.net in say as want you 【未经授权禁止转载】【版权所有】唐霜 www.tangshuang.netwant.

【版权所有】唐霜 www.tangshuang.net【作者:唐霜】著作权归作者所有,禁止商业用途转载。