首页 > 解决方案 > 如何在 es6 中使用箭头函数而不是 bind 方法

问题描述

以前我用它在 React JSX 组件中调用我的方法,这个方法给了我正确的输出

this.updateState.bind(this)

但是当我将上述语句替换为

() => this.updateState(...this)

这没有给我输出它返回未定义

标签: javascriptreactjsecmascript-6

解决方案


你应该更换

 () => this.updateState(...this)

 (...args) => this.updateState(...args)

箭头函数从其父词法范围继承其上下文。
从箭头函数调用函数时,它继承自调用者的“this”引用。


推荐阅读