noteslearnings

How does React remember paused work?

What needs to remain so a calculation can pause and resume? Following Fiber helped me distinguish work in progress from the UI currently on screen.

The explanation that React can pause UI calculation raised a question. If calculation is descending through function calls when it stops, how does React know where to resume later? What stays on screen in the meantime?

Fiber stores work and its progress in objects. What has been processed and where to go next are not left solely to the function call stack. Representing progress this way lets interruptible rendering yield between units of work. Behind the ability to pause and resume was a structure that represents work so it can be continued.

Keeping the screen intact needed a separate look too. React distinguishes Current, the basis of the UI already applied, from Work-in-Progress, which prepares the next UI. Instead of mixing each calculated piece into the screen as it becomes available, it keeps the existing UI and applies the prepared changes during Commit. This connected with the browser’s structure of retaining an existing result while preparing a new display.

Could a node without an update queue simply be skipped, then? Even without its own state change, values passed by a parent or context may change, and descendants may have work pending. One queue alone cannot settle the question. Keeping change requests and deciding whether an existing result can be reused are different tasks.

This also limited my expectation that Fiber would automatically split up every long calculation. It does not cut a running component function at an arbitrary line. I came to understand pausing and resuming less as a magical feature than as a design for remembering work and protecting the current result.

Lecture: A Cartoon Intro to Fiber, Lin Clark