小编Bri*_*Bui的帖子

如何在 debounce 中传入参数

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)

javascript closures arguments debouncing

7
推荐指数
1
解决办法
3402
查看次数

setState 接受函数与对象

假设我有一个想要更新的状态:

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)

setstate reactjs

1
推荐指数
1
解决办法
1125
查看次数

标签 统计

arguments ×1

closures ×1

debouncing ×1

javascript ×1

reactjs ×1

setstate ×1