All posts

Which render does useEffectEvent read its latest values from?

, 11 min, react, useEffect, useEffectEvent, useRef

useEffectEvent reads the latest values while keeping a subscription alive. A reproduction and the React source show why it reads a different value from a ref during an uncommitted render.

On this page7
  1. How does a useEffect callback read state?
  2. Adding theme to the dependencies also reconnects the room
  3. Separate when to resubscribe from what to read when a message arrives
  4. Before commit, it reads a different value from a ref
  5. Store the next implementation during rendering, then replace it during commit
  6. Calling it during rendering throws an error
  7. A different function on each render can still read the latest values