main-thread-scheduling
main-thread-scheduling copied to clipboard
interrupt the rendering of react
This project is great, I would like to ask whether it can interrupt the rendering of react. For example, if there is a list of 10,000 length arrays and react updates this array when performing state updates, can it interrupt the rendering
Hey, good question. I've been asked this before. Maybe I should add a FAQ section and include this question.
You can't interrupt the React rendering. However, you can:
- use the new concurrent rendering features in React 18 —
useDeferredValue(),startTransition(), anduseTransitionthat can render without blocking the UI. React has their own scheduler that has similarities with whatmain-thread-schedulingis doing. - gradually render your results by calling
setState()in React after eachyieldOrContinue()call.
gradually render your results by calling setState() in React after each yieldOrContinue() call.
However, this needs to be done carefully, because React is able to batch state updates. If done blindly, yielding after each setState might produce more re-renders than needed (= worse performance ultimately).
Ah, you are right. I've started writing an FAQ section with a more nuanced answer and information on the topic. Thanks!