最佳答案
对于类组件,如果内部事件处理程序,则 this.setState
调用批处理。但是,如果在事件处理程序之外更新状态并使用 useState
钩子,会发生什么情况呢?
function Component() {
const [a, setA] = useState('a');
const [b, setB] = useState('b');
function handleClick() {
Promise.resolve().then(() => {
setA('aa');
setB('bb');
});
}
return <button onClick={handleClick}>{a}-{b}</button>
}
它会立即呈现 aa - bb
吗? 还是先呈现 aa - b
,然后再呈现 aa - bb
?