I have code that looks something like:
// File1
async function fetchData() {
const data = await fetch(...);
setState({ data });
return data;
}
// File2
useEffect(() => {
(async () => {
const data = await fetchData();
setState({ data });
})();
});
This triggers 2 React commits in 1 task. This makes my app less than 60FPS. Ideally, I'd like to batch the 2 setState
s. Currently, it looks like this:
Pink represents React commits (DOM operations). The browser doesn't have a chance to repaint until the second commit is done. I can give the browser a chance to repaint by adding await new Promise(succ => setTimeout(succ, 0));
between the setStates
, but it'll be better if I could batch the commits.
It's also pretty much impossible to refactor this, since the useState
exists in separate files.
I tried unstable_batchedUpdates
but it doesn't work with async.
from Batching React updates across microtasks?
No comments:
Post a Comment