Confusing rendering order #2859
lgharibashvili
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I noticed that full R3F tree inside Canvas is rendered separately from the containing DOM elements.
When rendering DOM elements, we are used to the fact that React Reconciler recursively goes down the JSX tree, rendering elements bottom-up.
For example:
In this case it would render elements in the following order: 3, 2, 1.
However, if we add a Canvas element, we get a different order:
In this case the order becomes: 3, 2, 1, 5, 4.
Basically, DOM tree is rendered fully and then it moves on to R3F tree.
I created a little codesandbox to illustrate it:
https://codesandbox.io/s/react-three-fiber-rendering-order-26qekl?file=/src/index.tsx
This can be problematic when using useRef, because if we are passing ref object from containing component, it will be attached late and ref.current will be undefined during useEffect (as opposed to passing ref to DOM elements).
I created another codesandbox to illustrate that:
https://codesandbox.io/s/react-three-fiber-rendering-order-forked-qpjpc5?file=/src/index.tsx
This problem can be sidestepped by using useRef from a component that is inside R3F tree (inside Canvas element).
I wonder if this behavior is intended or if it is an unintended side effect of the way react reconciler works.
Beta Was this translation helpful? Give feedback.
All reactions