React-Native:我们可以直接使用 JavaScript 的 'debounce' 功能还是只能使用 _lodash 的 debounce?
React-Native: Can we use JavaScript's 'debounce' function directly or _lodash's debounce is the only option?
当我尝试使用 JavaScript's 'debounce'
时,如下所示,
debounce(() => {
this.getDataFn(true);
}, 3000);
收到类似 debounce is not defined
的错误。这个错误很明显,因为 react-native
正在将 debounce
关键字视为普通变量。
任何人都可以确认,_loadash's debounce
是唯一的选择还是任何替代方案而不需要打包?
debounce
功能不是开箱即用的。如果你不想添加单独的包,你可以实现自己的 debounce 功能,如下所示
const debounce = (fn, time) => {
let timeout;
return function() {
const functionCall = () => fn.apply(this, arguments);
clearTimeout(timeout);
timeout = setTimeout(functionCall, time);
}
}
如需完整参考,请查看 Medium
当我尝试使用 JavaScript's 'debounce'
时,如下所示,
debounce(() => {
this.getDataFn(true);
}, 3000);
收到类似 debounce is not defined
的错误。这个错误很明显,因为 react-native
正在将 debounce
关键字视为普通变量。
任何人都可以确认,_loadash's debounce
是唯一的选择还是任何替代方案而不需要打包?
debounce
功能不是开箱即用的。如果你不想添加单独的包,你可以实现自己的 debounce 功能,如下所示
const debounce = (fn, time) => {
let timeout;
return function() {
const functionCall = () => fn.apply(this, arguments);
clearTimeout(timeout);
timeout = setTimeout(functionCall, time);
}
}
如需完整参考,请查看 Medium