Closed mission-minnow closed 1 month ago
Looks like it's reusing the canvas but creating a second scene: https://codesandbox.io/s/morning-star-etwzsr?file=/src/App.tsx
However I run the following code locally and it does not seem to call the onSceneMount
twice. I am running react-babylonjs
@ 3.1.3
.
I think it's because you are in Strict
mode -- and thank-you for the helpful sandbox to reproduce.
Yes, confirmed with this snippet:
// in index.tsx
root.render(
<App />
);
Somewhat related I think - I just encountered a problem after upgrading to React 18 (which in strict mode runs each effect twice to check for proper clean-up). My problem is with enabling physics. When strict mode is enabled, I get an error from the AmmoJS plugin (this.world is null
) . Removing strict mode removes the problem, but it suggests that there should be some fixes to the main useEffect
in Scene.tsx
?
I gotta look more into this… The irony is that it was changed not long ago to use hooks!!
I have done some React 18.0-18.2 compatibility changes this week. It's working in StrictMode on the sandboxes I have tried. If you can share a reproduction then I will be happy to look at solutions. AmmoJS should allow re-creating scenes - so perhaps something in your code has made assumptions otherwise?
closing from inactivity. looks resolved and due to strict mode. thanks for your contribution.
Here's the code sandbox: https://codesandbox.io/s/quiet-haze-xyodgq?file=/src/App.tsx
In this simple example, I'm just creating a scene with a non-default clear color. If I log when the scene gets mounted, I get two different scenes.
Check the console output.
onSceneMount={(args)=>console.log(args)}
{scene: Scene, canvas: HTMLCanvasElement} {scene: Scene, canvas: HTMLCanvasElement}