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

WebpackError: ReferenceError: window is not defined #12

Open
petercampanelli opened this issue Oct 31, 2019 · 2 comments
Open

WebpackError: ReferenceError: window is not defined #12

petercampanelli opened this issue Oct 31, 2019 · 2 comments

Comments

@petercampanelli
Copy link

Getting this error on build.
WebpackError: ReferenceError: window is not defined
Anyone else having the same issue?

@create-juicey-app
Copy link

I have fixed it by making it clientside only
Next.js Example

// pages/index.js
import React from 'react';
import dynamic from 'next/dynamic';

const OBJModel = dynamic(
  () => import('react-3d-viewer').then((module) => module.OBJModel),
  { ssr: false }
);

const HomePage = () => {
  return (
    <div>
      {OBJModel && (
        <OBJModel
          src="/path/to/model.obj"
          width={500}
          height={500}
          position={{ x: 0, y: 0, z: 0 }}
        />
      )}
    </div>
  );
};

export default HomePage;

React Example

import React, { useRef, useEffect } from 'react';
import { OBJModel } from 'react-3d-viewer';

const ThreeJsComponent = () => {
  const mountRef = useRef(null);

  useEffect(() => {
    if (typeof window !== 'undefined') {
      mountRef.current.appendChild(document.createElement('div'));
    }
  }, []);

  return (
    <div ref={mountRef}>
      {/* Render the OBJModel component */}
      <OBJModel
        src="/path/to/model.obj"
        width={500}
        height={500}
        position={{ x: 0, y: 0, z: 0 }}
      />
    </div>
  );
};

export default ThreeJsComponent;

@create-juicey-app
Copy link

sddkds
we can now close the issue.

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

2 participants