React 中 调用 lodash.debounce 不错误绑定 this 解决方案

1,906 阅读1分钟

1、部分引入 lodash

import debounce from 'lodash/debounce';

2、绑定 this 致使 function 里使用 this 正常调用

ps: 不正确绑定 this 会导致后面 cannot find something of undefined 的报错

绑定 this 引用了这篇 文章

<Button 
    onClick={debounce(this.changeMapType.bind(this), 200)}>
    {this.state.chartType === 1 ? '桑基图' : '树图'}
</Button>

注意

debounce 需要在 onClick 里直接添加 箭头函数会导致实现