Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

React里面的这个bind(this)是什么意思?

clipboard.png

求详解。。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

题主想知道的应该是这里为什么要用bind。

  1. 首先bind就是改变函数运行时的this,详细请看bind

  2. 你的代码中调用了setInterval(fun, time),其实等同于window.setInterval(fun, time)

  3. 如果没调用bind(this),那么传入setInterval中的fun的this指向是setInterval的调用者window对象

  4. 给函数bind(this),这里的this指向就是componentDidMount()中的this也就是这个react对象,这样才能够正确访问react属性方法this.state,this.setState等


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...