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

ReferenceError: window is not defined #271

Closed
robylosa94 opened this issue May 8, 2020 · 4 comments
Closed

ReferenceError: window is not defined #271

robylosa94 opened this issue May 8, 2020 · 4 comments

Comments

@robylosa94
Copy link

robylosa94 commented May 8, 2020

I'm working with nextjs and threejs.
My configuration is:

import * as dat from "dat.gui";
const gui= new dat.GUI();
gui.add(settings, "colorful");
gui.add(settings, "zoom");
gui.add(settings, "random");

@bastienrobert
Copy link

bastienrobert commented Oct 7, 2020

@robylosa94 Thanks for this contribution. Could you please explain how you fixed it or add a reference to a resolved issue?

@user28111999
Copy link

I would like to know this as well. I'm running ThreeJS with NextJS and React

@sean6bucks
Copy link

@bastienrobert @19992077
Ran into this as well, solved it for now by just using a dynamic import where my three.js script is being run, and since I am not running the three.js logic until I am on client-side anyway (triggered via a useEffect) I know that the window will be available.

const init = async () => {
   const dat = await import('dat.gui')
   const gui = new dat.GUI()
   // ... rest of the three.js code
}

...

const CanvasComponent = (props) => {
  useEffect(() => {
    init()
  }, [])

  return (
    <canvas id="webgl" />
  )
}

Additionally found that dat.GUI does not work well with Fast Refresh, and doesn't know to not keep adding new menus, but thats another issue.

@kimbaudi
Copy link

Additionally found that dat.GUI does not work well with Fast Refresh, and doesn't know to not keep adding new menus

To fix this issue, move gui variable declaration out of init() and useEffect(). Then check whether gui exists before instantiating dat.gui.

const CanvasComponent = (props) => {
   let gui;

   const init = async () => {
      const dat = await import('dat.gui')
      if (!gui) {
         gui = new dat.GUI()
         // ... rest of the three.js code
      }
   }
   useEffect(() => {
      init()
   }, [])

  return (
    <canvas id="webgl" />
  )
}

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

5 participants