在setTimeout 中执行 useState的回调,但是组件不重新渲染
menhal opened this issue · comments
云小飞 commented
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>
)
}
云小飞 commented
刚才发现,随着我在页面空白处瞎乱点,组件刷新了。 不明白,为啥点击事件会导致组件render
司徒正美 commented
setTimeout(() => {
setCurrent(current + 1);
}, 1000);
不支持这样写,你会扰乱它的顺序