This creates a new three-renderer and a new frameloop on every prop change, or when the component gets triggered, youâre pulling the rug. It makes little sense to use three in react like that because youâre mixing two conflicting worlds: oop and fp, loosing all interop. I would suggest you use react-three-fiber: Basic demo - CodeSandbox
And if we donât want to use react-three-fiber, what would be the proposed solution? Also, I donât think thatâs correct since I put a console.log(âuseEffectâ) in my useEffect() and it does not log. My stack trace looks a bit different.
in that case i wouldnât use react. if you donât want <div> but document.createElement(âdivâ) thatâs vanilla js. ask yourself if not wanting react-dom in react makes any sense at all. the same exact thing applies to three. in the end mixing imperative and declarative is whatâs causing the issues here, and always will, using react is the only sensible solution, itâs literally why it exists.
as for use effects, you can read about it here. the same applies to suspense or scheduling, react can unmount components at will, if your useEffect has side effects but no clean up phase you risk race conditions and double execution. as for logs, iâm not sure if thatâs still true but react would hijack console.log, shutting it off so you didnât see two logs but the effect ran twice anyway.
I was getting this error and my solution was to cancel the animation frame when my useEffect returns.
âRequestAnimationFrame returns a long integer value, the request id, that uniquely identifies the entry in the callback list. This is a non-zero value, but you may not make any other assumptions about its value. You can pass this value to window.cancelAnimationFrame() to cancel the refresh callback request.â
Itâs ok for small projects but as you implement more features you will face more issues. Iâm following your suggestion and am going to use react-three-fiber drcmda.
I stumbled upon this error whilst playing around with Three.js recently. My fix was to save an instance of WebGLRenderer in a useRef object so it is skipped across reactâs lifecycle updates.
create a new reference directly to a DOM element react useRef
const rendererRef = useRef<WebGLRenderer>();
instantiate WebGLRenderer and update renderRefâs current value.