Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated GUI with vite + react + three.js #323

Open
Creative-Ataraxia opened this issue Sep 21, 2022 · 3 comments
Open

Duplicated GUI with vite + react + three.js #323

Creative-Ataraxia opened this issue Sep 21, 2022 · 3 comments

Comments

@Creative-Ataraxia
Copy link

Creative-Ataraxia commented Sep 21, 2022

I'm trying to use dat.gui with vite + react + tailwind + three.js:

function App() {
  useEffect(() => {
    const gui = new dat.GUI({ width: 400 })
    gui.add(..etc..)

  return (
    <>
      <canvas className='fixed top-0 left-0 outline-none' id="MyCanvas" />
    </>
  )
}

but the gui is duplicated, showing another panel on the back:
1
and if I click on open, it will open two sets of controls like so:
2
one set of control do work as intended, the other set does not work at all;

I've tried to pass the autoPlace: false but then the gui won't show up at all anymore

any help is much appreciated

edit:
also each time vite re-render's the page, a new set of gui is added to the viewport:
3
so it will become unusable like this:
4

do I have to unmount gui after each render somehow?
btw, I'm putting all the three.js code in a useEffect() hook to be able to render it via react..

edit2:
so on a first glance this seeks to be an issue of gui.dat not dismounting and/or removing the last <div> created on the dom when vite rebuilds the build as I save code changes in the IDE;
does dat.gui have a built-in API to remove the last created DOM element?

@rickyzhangca
Copy link

can confirm this happening on vite + react

@lesha-co
Copy link

lesha-co commented Dec 2, 2023

or you can put gui in a ref
or you can call gui.destroy() in useEffect return (just make sure it doesn't trigger unless needed to)

anyway this looks more like react issue than library issue

@Kvisaz
Copy link

Kvisaz commented Dec 24, 2023

It is normal behavior for useEffect with creation/subscription

To avoid bugs - always use return in useEffect

We need return some callback to unsubscribe, to destroy etc....

useEffect(() => {
    const gui = new dat.GUI({ width: 400 })
    gui.add(..etc..)
    return ()=> gui.destroy()
  }  
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants