运行 情况 col 有时 code urn ole ret
- render() {
- return (
- this.clickHandler.bind(this)}>vz
- )
- }
- clickHandler() {
- console.log(this.clickHandler);
- }
上面标签里若不使用 bind 的话,点击 p 标签会报错,因为 clickHandler 运行时 this 不指向 p 标签,若要指向 p 标签,就需要 bind 绑上。
其实有时候不用 bind 改变函数作用域也没问题,哪种情况呢,就是 clickHandler 定义里不涉及到 this 指向,如上面的 clickHandler 里改成 console.log('12345')。
React 为啥很多类里的标签上事件处理函数要用 bind(this)
来源: http://www.bubuko.com/infodetail-2146822.html