All posts

useRef holds values not needed for rendering, so why should you not read it during render?

, 6 min, react, useRef, rendering

Two sentences in the React docs looked contradictory at first. useRef stores values not needed for rendering, yet React says not to read ref.current during render. Tracing the source code shows these are about two different dimensions of rendering: trigger vs consistency, all the way to tearing.

On this page11
  1. Two dimensions of “rendering”
  2. Structural difference in source code
  3. useRef — no update queue
  4. useState — update queue + scheduleUpdateOnFiber
  5. What goes wrong if you read during render
  6. Problem 1: It does not show up on screen
  7. Problem 2: Accidental reflection
  8. Problem 3: Tearing
  9. Why is initialization an exception?
  10. Why isn't this rule enforced by code?
  11. Wrap-up