2

I am loading multiple 3D elements in my pages, so to avoid creating multiple canvases I use react-three-drei's View.
My <Canvas> is inside App.js and outside of my react-router-dom's <Routes>.

The problem is upon changing route the <OrbitControls> appear to break as I cannot move the object around with the mouse anymore.

enter image description here

App.js

export default function App() {
  const [canvasWrapperRef, canvasView1, canvasView2] = useRefs();
  const canvasRefs = { canvasView1, canvasView2 };

  return (
    <div className="App">
      <div ref={canvasWrapperRef}>
        <Suspense fallback={null}>
          <Routes>
            <Route path="/" element={<Homepage ref={canvasRefs} />} />
            <Route path="/other" element={<OtherPage ref={canvasRefs} />} />
          </Routes>
          <Canvas
            onCreated={(state) => {
              console.log("canvas created");
              state.events.connect(canvasWrapperRef.current);
            }}
            className="canvas"
          >
            <View track={canvasView1}>
              <Scene />
              <TransformControls>
                <Soda scale={6} position={[0, -1.6, 0]} />
              </TransformControls>
              <OrbitControls makeDefault />
            </View>
            <View track={canvasView2}>
              <color attach="background" args={["lightblue"]} />
              <Scene />
              <TransformControls position={[0, -1, 0]}>
                <Duck scale={2} position={[0, -1.6, 0]} />
              </TransformControls>
              <OrbitControls makeDefault />
            </View>
            <Preload all />
          </Canvas>
        </Suspense>
      </div>
    </div>
  );
}

function Scene() {
  return (
    <>
      <ambientLight intensity={1} />
      <Environment preset="dawn" />
    </>
  );
}

Homepage.js

const Homepage = React.forwardRef((props, ref) => {
  const { canvasView1, canvasView2 } = ref;

  return (
    <>
      <h1>Homepage</h1>
      <Link to="/other">Other Page</Link>
      <p>Position 1 - Bottle</p>
      <div ref={canvasView1} style={{ height: "100px", width: "100px" }} />
      <p>Position 2 - Apple</p>
      <div ref={canvasView2} style={{ height: "100px", width: "100px" }} />
    </>
  );
});

OtherPage.js

const OtherPage = React.forwardRef((props, ref) => {
  const { canvasView1, canvasView2 } = ref;

  return (
    <>
      <div ref={canvasView1} />
      <h1>Other Page</h1>
      <Link to="/">Homepage</Link>
      <br />
      <p>Position 3 - Duck</p>
      <div
        ref={canvasView2}
        style={{
          width: "100px",
          height: "200px",
          display: "inline-block"
        }}
      />
    </>
  );
});

I tried seeing if <Canvas> persists or not (as that could be the cause) by doing a console.log() on the onCreated property of <Canvas>, but it only logs once, so I assume it doesn't get unmounted when I switch pages, so that can't be the cause.

Edit react router v6 example (forked)

Thank you in advance for any suggestion.

Monstar
  • 795
  • 1
  • 5
  • 23
  • 1
    You have to add the code in the question. Links to external resources tend to break or the content may change. See [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error **and the shortest code necessary to reproduce it in the question itself.**"* – Rabbid76 Sep 20 '22 at 18:49
  • Your example (on sandbox) is working for me (I can't reproduce the issue) Maybe the issue is somewhere else? Or try incognito to see if is working? – Berci Sep 27 '22 at 08:47
  • @Berci I did some testing on the codesandbox without saving it to see if detecting the mouse position works but it seems it still got saved by codesandbox... I just reverted the changes to the initial code! – Monstar Sep 27 '22 at 15:05

0 Answers0