javascript - 如何在 es6 中使用箭头函数而不是 bind 方法
问题描述
以前我用它在 React JSX 组件中调用我的方法,这个方法给了我正确的输出
this.updateState.bind(this)
但是当我将上述语句替换为
() => this.updateState(...this)
这没有给我输出它返回未定义
解决方案
你应该更换
() => this.updateState(...this)
和
(...args) => this.updateState(...args)
箭头函数从其父词法范围继承其上下文。
从箭头函数调用函数时,它继承自调用者的“this”引用。
推荐阅读
- vue.js - Ionic Vue 幻灯片错误?我错过了什么?
- angular - 隐藏圆环图 ng2-charts 上的图例
- javascript - 有没有办法在 ChartXY 上锁定纵横比?
- python - 如何遍历列中的每 N 行,并将值作为不带括号的列表传递 URL?
- python - 游戏的问题
- javascript - createElement() 无法处理基本路径元素
- python - 尝试计算正确的回报并使用“quad_form”设置对每种资产的最大和最小投资限制
- python - Librosa 安装 pip 失败
- swift - 如何在 SwiftUI 的 body 属性中显示函数中存在的值?
- python - spaCy 错误地将手指识别为动词