# renderer.render() method makes an WebGL error in useEffect

**URL:** <https://discourse.threejs.org/t/renderer-render-method-makes-an-webgl-error-in-useeffect/40866>\
**Category:** Questions\
**Tags:** react\
**Created:** [July 30, 2022, 6:37am UTC](https://discourse.threejs.org/t/renderer-render-method-makes-an-webgl-error-in-useeffect/40866 "2022-07-30T06:37:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NEARworld](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/nearworld/32/28412_2.png) [@NEARworld](https://discourse.threejs.org/u/NEARworld)\
**Post date:** [July 30, 2022, 6:37am UTC](https://discourse.threejs.org/t/renderer-render-method-makes-an-webgl-error-in-useeffect/40866/1 "2022-07-30T06:37:05Z")

</div>

```javascript
import React, { useEffect, useRef } from 'react';
import './App.css';
import * as THREE from 'three'

function App() {
  console.log('App');

  const threeCanvas = useRef<HTMLCanvasElement>(null);
  useEffect(() => {
    const scene = new THREE.Scene()
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
    camera.position.set(0, 0, 50)
    const canvas = threeCanvas.current
    if (!canvas) return
    const boxGeometry = new THREE.BoxGeometry(10, 10, 10)
    const boxMaterial = new THREE.MeshNormalMaterial()
    const boxMesh = new THREE.Mesh(boxGeometry, boxMaterial)
    const renderer = new THREE.WebGLRenderer({ canvas })
    scene.add(boxMesh)
    const animate = () => {
      boxMesh.rotation.x += 0.01;
      boxMesh.rotation.y += 0.01;
      renderer.render(scene, camera)
      window.requestAnimationFrame(animate)
      // console.log('animate');

    }
    animate()
  })
  return (
    <div className="App">
      <canvas id="threeCanvas" ref={threeCanvas} />
    </div>
  );
}

export default App;

```

error in console.

```javascript
three.module.js:17398 WebGL: INVALID_OPERATION: uniformMatrix4fv: location is not from current program
setValueM4 @ three.module.js:17398
setValue @ three.module.js:18026
setProgram @ three.module.js:28321
WebGLRenderer.renderBufferDirect @ three.module.js:27254
renderObject @ three.module.js:27862
renderObjects @ three.module.js:27831
renderScene @ three.module.js:27753
WebGLRenderer.render @ three.module.js:27573
animate @ App.tsx:23
requestAnimationFrame (async)
animate @ App.tsx:24
requestAnimationFrame (async)
animate @ App.tsx:24
(anonymous) @ App.tsx:28
commitHookEffectListMount @ react-dom.development.js:23150
commitPassiveMountOnFiber @ react-dom.development.js:24926
commitPassiveMountEffects_complete @ react-dom.development.js:24891
commitPassiveMountEffects_begin @ react-dom.development.js:24878
commitPassiveMountEffects @ react-dom.development.js:24866
flushPassiveEffectsImpl @ react-dom.development.js:27039
flushPassiveEffects @ react-dom.development.js:26984
(anonymous) @ react-dom.development.js:26769
workLoop @ scheduler.development.js:266
flushWork @ scheduler.development.js:239
performWorkUntilDeadline @ scheduler.development.js:533

```

---

<div class="post-metadata">

**Author:** ![drcmda](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@drcmda](https://discourse.threejs.org/u/drcmda)\
**Post date:** [July 30, 2022, 6:58am UTC](https://discourse.threejs.org/t/renderer-render-method-makes-an-webgl-error-in-useeffect/40866/2 "2022-07-30T06:58:00Z")

</div>

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](https://codesandbox.io/s/rrppl0y8l4)

---

<div class="post-metadata">

**Author:** ![FullstackJack](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/fullstackjack/32/32876_2.png) [@FullstackJack](https://discourse.threejs.org/u/FullstackJack)\
**Post date:** [January 28, 2023, 1:53am UTC](https://discourse.threejs.org/t/renderer-render-method-makes-an-webgl-error-in-useeffect/40866/3 "2023-01-28T01:53:04Z")

</div>

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.

```javascript
setValueM4	@	three.module.js:17409
setValue	@	three.module.js:18165
setProgram	@	three.module.js:28561
WebGLRenderer.renderBufferDirect	@	three.module.js:27632
renderObject	@	three.module.js:28263
renderObjects	@	three.module.js:28232
renderScene	@	three.module.js:28154
WebGLRenderer.render	@	three.module.js:27974
CanvasManager.animate	@	CanvasManager.ts:262
(anonymous)	@	CanvasManager.ts:265
requestAnimationFrame (async)		
CanvasManager.animate	@	CanvasManager.ts:265
(anonymous)	@	CanvasManager.ts:265
requestAnimationFrame (async)		
CanvasManager.animate	@	CanvasManager.ts:265
(anonymous)	@	CanvasManager.ts:265
requestAnimationFrame (async)
...

```

---

<div class="post-metadata">

**Author:** ![drcmda](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@drcmda](https://discourse.threejs.org/u/drcmda)\
**Post date:** [January 28, 2023, 9:43am UTC](https://discourse.threejs.org/t/renderer-render-method-makes-an-webgl-error-in-useeffect/40866/4 "2023-01-28T09:43:27Z")

</div>

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](https://beta.reactjs.org/reference/react/StrictMode). 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](https://camunda.com/blog/2021/02/be-careful-with-console-log-when-using-react-strictmode/), shutting it off so you didn’t see two logs but the effect ran twice anyway.

---

<div class="post-metadata">

**Author:** ![9uifranco](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/9uifranco/32/33590_2.png) [@9uifranco](https://discourse.threejs.org/u/9uifranco)\
**Post date:** [February 17, 2023, 2:30am UTC](https://discourse.threejs.org/t/renderer-render-method-makes-an-webgl-error-in-useeffect/40866/5 "2023-02-17T02:30:02Z")

</div>

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()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/cancelAnimationFrame) to cancel the refresh callback request.”

[More info here](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)

So I created a variable

```javascript
let myReq

```

Got the request ID

```javascript
const animate = () => {
      .
      .
      .
      myReq = window.requestAnimationFrame(animate)
    }
animate()

```

Added a return on my useEffect with the cancelAnimationFrame() function and passed the request ID as param

```javascript
useEffect(() => {
  .
  .
  .
  return () => { 
    window.cancelAnimationFrame(myReq)
  }
}, [])

```

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](https://discourse.threejs.org/u/drcmda).

---

<div class="post-metadata">

**Author:** ![Ify](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ify/32/43152_2.png) [@Ify](https://discourse.threejs.org/u/Ify)\
**Post date:** [December 19, 2023, 11:34pm UTC](https://discourse.threejs.org/t/renderer-render-method-makes-an-webgl-error-in-useeffect/40866/6 "2023-12-19T23:34:55Z")

</div>

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](https://react.dev/reference/react/useRef)

```javascript
const rendererRef = useRef<WebGLRenderer>();

```

instantiate **WebGLRenderer** and update **renderRef’s** current value.

```javascript
  useEffect(() => {
    if (canvas.current) {
      const webGLRenderer = new THREE.WebGLRenderer({
        canvas: canvas.current,
      });

      webGLRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
      webGLRenderer.setClearAlpha(0.1);

      renderer.current = webGLRenderer;
    }
  }, [canvas.current]);

```

Here is a working codesandbox [link](https://codesandbox.io/p/sandbox/threejs-sample-project-th83kn)
