Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind this #10

Open
ZWkang opened this issue May 31, 2018 · 0 comments
Open

bind this #10

ZWkang opened this issue May 31, 2018 · 0 comments

Comments

@ZWkang
Copy link
Owner

ZWkang commented May 31, 2018

我们经常使用React可以发现组件内部方法调用this时候 一般都不会是组件实例
我们可以发现this可用一般在componentDidMount与render内可用
别的组件方法常常不可用this
这与js的机制有关系

总结我们稳固this的方法

React.createClass
this的bind统一放在constructor中bind
constructor(){
    this.xxx = this.xxx.bind(this)
}
//实例时候绑定this
定义方法使用箭头函数
xxx = ()=>{}
render调用处bind
onClick = {this.xxx.bind(xxx)}
render调用处使用箭头绑定 this
onClick = {(e)=>{this.xxx(e)}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant