const debounce = (func) => {
return (arg) => {
let timeoutId;
if (timeoutId){
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
func(arg);
}, 1000);
}
}
function hello(name){
console.log("hello " + name);
}
input.addEventListener("input", debounce(hello));
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我该如何去抖并hello使用 debounce 和 name 调用该函数"Brian"。
在代码行 2 上,return (arg) => {在变量中传递参数的代码是什么?
我知道它debounce(hello);调用了 debounce 函数,但是我该如何传递一个变量以便将其存储在中(arg)?
假设我有一个想要更新的状态:
state = {
description: "",
notes: ""
}
Run Code Online (Sandbox Code Playgroud)
如果我不需要使用 prevState,只用一个对象更新状态,有什么区别吗?
this.setState({description: "Blue"});
Run Code Online (Sandbox Code Playgroud)
与
this.setState((prevState)=> ({description: "Blue"}));
Run Code Online (Sandbox Code Playgroud)