effectussoftware / react-custom-roulette

Repository for the react-custom-roulette library
MIT License
312 stars 112 forks source link

Error building next js app #135

Open spandres opened 1 year ago

spandres commented 1 year ago

Problem

I get the following error message after running npm run build:

ReferenceError: window is not defined

Description

I'm importing from a client component ("use client") the wheel component. Unfortunately, the build fails. I verified the problem was the wheel component because after removing it, the build was completed as expected.

I tried the wheel component using the basic setup in the README.

The wheel component works when I run the npm run dev command. The problem is observed only when trying to build the app.

Screenshot 2023-08-02 at 20 21 26
Gabriel-Passoss commented 1 year ago

Same issue

spandres commented 1 year ago

My workaround was to create a wrapper component at the layout level.

export default function Wrapper({ children }: {children?: ReactNode}) {
  const [ready, setReady] = useState(false);

  useEffect(() => {
    setReady(true);
  }, []);

  if (!ready) return null;

  return <main>{children}</main>;
}

Just put the wheel component anywhere inside the wrapper, and it should work.

supramaxis commented 11 months ago

My workaround was to create a wrapper component at the layout level.

export default function Wrapper({ children }: {children?: ReactNode}) {
  const [ready, setReady] = useState(false);

  useEffect(() => {
    setReady(true);
  }, []);

  if (!ready) return null;

  return <main>{children}</main>;
}

Just put the wheel component anywhere inside the wrapper, and it should work.

did not worked for me sadly on nextjs

berthelol commented 9 months ago

This one worked for me https://www.npmjs.com/package/react-wheel-of-prizes

abdulsami822 commented 6 months ago

Any update on this issue?

buivangiang-12148701 commented 5 months ago

worked for me:

Modify your import statement for the Wheel component to use Next.js is dynamic imports with ssr set to false. This tells Next.js to only render the component on the client side.

Old: import { Wheel } from 'react-custom-roulette';

Replace:

import dynamic from 'next/dynamic'; const Wheel = dynamic(() => import('react-custom-roulette').then((mod) => mod.Wheel), { ssr: false, });

const WheelOfLuckyGame = (props) => { ... . return ( <Wheel ...../> )}