手动批量处理
unstable_batchedUpdates
可用于手动批量更新state,可以指定多个setState合并为一个更新请求。
import { useState, useLayoutEffect } from "react";
import { unstable_batchedUpdates } from "react-dom";function App() {const [count, setCount] = useState(0);const [value, setValue] = useState(false);function handleClick() {setTimeOut(() => {ReactDOM.unstable_batchedUpdates(() => {setCount((c) => c + 1); // Causes a re-rendersetValue((f) => !f); // Causes a re-render})}, 100)}return (<div><button onClick={handleClick}>Next</button></div>);
}