dwqdaiwenqi / react-3d-viewer

A 3D model viewer component based on react.js 一个基于react.js的组件化3d模型查看工具
165 stars 64 forks source link

WebpackError: ReferenceError: window is not defined #12

Open petercampanelli opened 4 years ago

petercampanelli commented 4 years ago

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

create-juicey-app commented 7 months ago

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 commented 7 months ago

sddkds we can now close the issue.