DEV Community

Discussion on: A Concurrent Mode-Safe Version of useRef

Collapse
 
reaysawa profile image
João Paulo

Thanks for sharing!

I'm curious about this line

Promise.resolve().then(()=> raw.isRendering = false)

What guarantees do we have that the promise will only resolve in the commit phase of React?

As far as I'm aware, only useLayoutEffect is guaranteed to be ran once in the commit phase. See this video: youtube.com/watch?v=V1Ly-8Z1wQA at 16:05.

If that's correct, then we could change it to something like:

  raw.isRendering = true;
  raw.currentValue = raw.comittedValue;
  useLayoutEffect(() => {
    raw.isRendering = false;
    raw.comittedValue = raw.currentValue;
  });
Enter fullscreen mode Exit fullscreen mode

And I think that would work together better with React's guarantees. What do you think?

Collapse
 
eddiecooro profile image
Eddie

Agree 👍
Also, if we assume this, what happens to time slicing?

Collapse
 
sarimarton profile image
Marton Sari

This doesn't work guys. Have you tested it?