brianzinn / react-babylonjs

React for Babylon 3D engine
https://brianzinn.github.io/react-babylonjs/
812 stars 102 forks source link

Two scenes created in simple example. #240

Closed mission-minnow closed 1 month ago

mission-minnow commented 1 year ago

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}

AJamesPhillips commented 1 year 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.

brianzinn commented 1 year ago

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 />
);
mz8i commented 1 year ago

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?

brianzinn commented 1 year ago

I gotta look more into this… The irony is that it was changed not long ago to use hooks!!

brianzinn commented 1 year ago

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?

brianzinn commented 1 month ago

closing from inactivity. looks resolved and due to strict mode. thanks for your contribution.