【发布时间】:2019-03-15 20:48:25
【问题描述】:
使用 npm debounce 我在 ReactJS 中收到以下代码错误。错误
Javascript - Uncaught TypeError: Object(...) is not a function
在函数被传入 debounce() 时发生
import React, { Component } from 'react';
import { debounce } from 'debounce';
class test extends Component {
constructor(props) {
super(props);
this.state = {
yolo: {}
};
}
foobar(param) {
debounce(() => {
this.setState({yolo: param});
}, 99);
}
render () {
return (
...
<SomeComponent action={this.foobar.bind(this)} />
...
);
}
}
我尝试了Perform debounce in React.js中提到的一些解决方案 但似乎没有一个工作。
【问题讨论】:
-
你能展示你的进口吗?
-
现在应该是
fobar = debounce((param) => ...);,你传递给 debounce 的函数永远不会被调用 -
@AndreKnob 完成
-
介意试试这个
import debounce from 'debounce';?我知道 github repo 说要使用其他语法,但请尝试一下。 -
@m.a.solano93 确实有效,还有 foobar = debounce(param => {}, 99);您介意创建答案来解释原因吗?我必须承认我不完全确定进口方式的不同。
标签: javascript reactjs npm debounce