why react component bind this in constructor

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

In many react compon【转载请注明来源】著作权归作者所有,禁止商业用途转载。ent’s construc转载请注明出处:www.tangshuang.net原创内容,盗版必究。tor, developers alwa转载请注明出处:www.tangshuang.net【本文首发于唐霜的博客】ys bind its methods 【未经授权禁止转载】【关注微信公众号:wwwtangshuangnet】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.net

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

【版权所有】唐霜 www.tangshuang.net转载请注明出处:www.tangshuang.net【访问 www.tangshuang.net 获取更多精彩内容】【作者:唐霜】【关注微信公众号:wwwtangshuangnet】
<a href="" onclick="alert(this.href)">click</a>

You know what this p著作权归作者所有,禁止商业用途转载。未经授权,禁止复制转载。oint to here, so whe转载请注明出处:www.tangshuang.net本文作者:唐霜,转载请注明出处。n you use a componen【版权所有】唐霜 www.tangshuang.net【作者:唐霜】t method as a event 【版权所有】唐霜 www.tangshuang.net本文版权归作者所有,未经授权不得转载。callback function, t原创内容,盗版必究。转载请注明出处:www.tangshuang.nethis in the function 【版权所有】唐霜 www.tangshuang.net【作者:唐霜】does not referer to 【原创内容,转载请注明出处】本文作者:唐霜,转载请注明出处。the instance of comp原创内容,盗版必究。著作权归作者所有,禁止商业用途转载。onent. So another wa【本文首发于唐霜的博客】【版权所有】唐霜 www.tangshuang.nety to solve this prob未经授权,禁止复制转载。【关注微信公众号:wwwtangshuangnet】lem is to bind this 【原创内容,转载请注明出处】【版权所有,侵权必究】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【原创不易,请尊重版权】【版权所有,侵权必究】 in say as want you 【访问 www.tangshuang.net 获取更多精彩内容】【原创内容,转载请注明出处】want.

【版权所有】唐霜 www.tangshuang.net【未经授权禁止转载】【关注微信公众号:wwwtangshuangnet】【关注微信公众号:wwwtangshuangnet】【未经授权禁止转载】