why react component bind this in constructor

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

In many react compon【版权所有,侵权必究】【关注微信公众号:wwwtangshuangnet】ent’s construc【未经授权禁止转载】【原创内容,转载请注明出处】tor, developers alwa转载请注明出处:www.tangshuang.net【版权所有】唐霜 www.tangshuang.netys 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未经授权,禁止复制转载。本文版权归作者所有,未经授权不得转载。s regulated by react【作者:唐霜】本文版权归作者所有,未经授权不得转载。?

转载请注明出处:www.tangshuang.net本文作者:唐霜,转载请注明出处。【未经授权禁止转载】转载请注明出处:www.tangshuang.net

No!【本文首发于唐霜的博客】

【访问 www.tangshuang.net 获取更多精彩内容】【作者:唐霜】【本文首发于唐霜的博客】【转载请注明来源】

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

本文作者:唐霜,转载请注明出处。【关注微信公众号:wwwtangshuangnet】原创内容,盗版必究。
<a href="" onclick="alert(this.href)">click</a>

You know what this p【版权所有】唐霜 www.tangshuang.net【作者:唐霜】oint to here, so whe本文版权归作者所有,未经授权不得转载。【本文首发于唐霜的博客】n you use a componen【访问 www.tangshuang.net 获取更多精彩内容】【版权所有】唐霜 www.tangshuang.nett method as a event 【版权所有,侵权必究】原创内容,盗版必究。callback function, t【原创内容,转载请注明出处】原创内容,盗版必究。his in the function 本文作者:唐霜,转载请注明出处。【原创内容,转载请注明出处】does not referer to 原创内容,盗版必究。【版权所有】唐霜 www.tangshuang.netthe instance of comp【访问 www.tangshuang.net 获取更多精彩内容】本文作者:唐霜,转载请注明出处。onent. So another wa转载请注明出处:www.tangshuang.net【未经授权禁止转载】y to solve this prob【未经授权禁止转载】本文版权归作者所有,未经授权不得转载。lem is to bind this 【访问 www.tangshuang.net 获取更多精彩内容】著作权归作者所有,禁止商业用途转载。when you use the met【未经授权禁止转载】本文版权归作者所有,未经授权不得转载。hod, like:

【关注微信公众号:wwwtangshuangnet】原创内容,盗版必究。本文作者:唐霜,转载请注明出处。著作权归作者所有,禁止商业用途转载。
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 未经授权,禁止复制转载。【原创不易,请尊重版权】want.

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