main-thread-scheduling icon indicating copy to clipboard operation
main-thread-scheduling copied to clipboard

interrupt the rendering of react

Open zhaozailin opened this issue 1 year ago • 3 comments

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

zhaozailin avatar Apr 30 '24 08:04 zhaozailin

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(), and useTransition that can render without blocking the UI. React has their own scheduler that has similarities with what main-thread-scheduling is doing.
  • gradually render your results by calling setState() in React after each yieldOrContinue() call.

astoilkov avatar Apr 30 '24 09:04 astoilkov

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).

kurtextrem avatar May 07 '24 14:05 kurtextrem

Ah, you are right. I've started writing an FAQ section with a more nuanced answer and information on the topic. Thanks!

astoilkov avatar May 07 '24 14:05 astoilkov