# How to shut down Babylon cleanly on React Route change?

**URL:** <https://forum.babylonjs.com/t/how-to-shut-down-babylon-cleanly-on-react-route-change/26158>\
**Category:** Questions\
**Created:** [December 18, 2021, 7:14am UTC](https://forum.babylonjs.com/t/how-to-shut-down-babylon-cleanly-on-react-route-change/26158 "2021-12-18T07:14:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brechindo](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@brechindo](https://forum.babylonjs.com/u/brechindo)\
**Post date:** [December 18, 2021, 7:14am UTC](https://forum.babylonjs.com/t/how-to-shut-down-babylon-cleanly-on-react-route-change/26158/1 "2021-12-18T07:14:34Z")

</div>

Im using Babylon wrapped in a React Component on a React Route. Everything’s fine until I try to navigate away from the route where Babylon runs. A simple Home (which works as expected from non-Babylon-enabled routes) sends the browser into a CPU-intensive tight loop where everything freezes and I have to close the tab.

Is there some kind of Babylon cleanup I should be doing on some kind of React route cleanup hook? Deleting the scene and engine and releasing the WebGL context somehow?

---

<div class="post-metadata">

**Author:** ![westonsoftware](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/westonsoftware/32/4216_2.png) [@westonsoftware](https://forum.babylonjs.com/u/westonsoftware)\
**Post date:** [December 19, 2021, 5:28pm UTC](https://forum.babylonjs.com/t/how-to-shut-down-babylon-cleanly-on-react-route-change/26158/2 "2021-12-19T17:28:21Z")

</div>

Try using … this.engine.dispose(), I’m using that to clean-up a vue.js project.  
–Andy

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [December 19, 2021, 6:59pm UTC](https://forum.babylonjs.com/t/how-to-shut-down-babylon-cleanly-on-react-route-change/26158/3 "2021-12-19T18:59:33Z")

</div>

Does this not cleanup correctly? You component should unmount when the route changes and call what is returned by `useEffect`.

```auto
import { useEffect } from 'react';

useEffect(() => {
  const engine = new Engine(...);
  return () => {
    engine.dispose();
}, [])

```

Have a look at the babylonjs hook npm code:  
[babylonjs-hook/babylonjs-hook.tsx at master · brianzinn/babylonjs-hook (github.com)](https://github.com/brianzinn/babylonjs-hook/blob/master/src/babylonjs-hook.tsx#L223)

---

<div class="post-metadata">

**Author:** ![brechindo](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@brechindo](https://forum.babylonjs.com/u/brechindo)\
**Post date:** [December 20, 2021, 5:00pm UTC](https://forum.babylonjs.com/t/how-to-shut-down-babylon-cleanly-on-react-route-change/26158/4 "2021-12-20T17:00:33Z")

</div>

Thanks for the suggestion. I do already have that call in component cleanup. B-( But okay, at least I know Im following SOP then. Must be something else. No warnings are emitting to console.

```auto
export default (props:IBabylonComponentProps) => {
    const reactCanvas = useRef(null);
    const { antialias, engineOptions, adaptToDeviceRatio, sceneOptions, onPreRender, onSceneReady, config, ...rest } = props;
    useEffect(() => {
        if (reactCanvas.current) {
            const engine = new Engine(reactCanvas.current, antialias, engineOptions, adaptToDeviceRatio);
            ...
            return () => {
                console.log( 'BabylonComponent dispose engine' );
                ***engine.dispose();***
                if (window) {
                    window.removeEventListener("resize", resize);
                }
            };
        }
    }, [reactCanvas]);

```

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [December 20, 2021, 5:36pm UTC](https://forum.babylonjs.com/t/how-to-shut-down-babylon-cleanly-on-react-route-change/26158/5 "2021-12-20T17:36:23Z")

</div>

can you share a repo or a code sandbox? it sounds like you are saying that you can see that being logged. I use react router and haven’t come across this issue before. Maybe react dev tools would be useful here to point out any component issues. cheers.

---

<div class="post-metadata">

**Author:** ![brechindo](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@brechindo](https://forum.babylonjs.com/u/brechindo)\
**Post date:** [December 21, 2021, 10:49pm UTC](https://forum.babylonjs.com/t/how-to-shut-down-babylon-cleanly-on-react-route-change/26158/6 "2021-12-21T22:49:46Z")

</div>

Found it! Apparently I created a double-entry bug in the scene which engine.dispose() does not take kindly.

Lesson learned: TransformNode constructor adds itself to your scene. Do not manually add it.

```auto
node = new TransformNode( "asdf", scene );
...
scene.addTransformNode( node );

```

I learned this behavior from LoadAssetContainer, where the loaded meshes DO need to be added to the scene. The reason for the disparity is not apparent to me.

Also, would be nice of course if Babylon failed more gracefully on this user error, for the sake of us n00bs.

Engine lockup problem gone.
