RubyLouvre / anu

the React16-compat library with hooks

Home Page:https://rubylouvre.github.io/anu/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

在setTimeout 中执行 useState的回调,但是组件不重新渲染

menhal opened this issue · comments

 var {useState} = React
function FuncComponent() {
    let [current, setCurrent] = useState(0);

    setTimeout(() => {
        setCurrent(current + 1);
    }, 1000);

    console.log("render:"+ current)

    return (
        <div>FuncComponent:{current} <button type={"button"} onClick={() => setCurrent( current + 1)}>Add</button></div>
    )
}

刚才发现,随着我在页面空白处瞎乱点,组件刷新了。 不明白,为啥点击事件会导致组件render

setTimeout(() => {
setCurrent(current + 1);
}, 1000);

不支持这样写,你会扰乱它的顺序