iamraphson / react-paystack

ReactJS library for implementing paystack payment gateway
https://www.npmjs.com/package/react-paystack
MIT License
445 stars 159 forks source link

Can't create a production build with package imported - Error occurred prerendering page; ReferenceError: window is not defined #112

Open RamazaniMwemedi opened 1 month ago

RamazaniMwemedi commented 1 month ago

Whenever I try to make a production build with this component imported, it keep throwing an error as shown below

image

I have troubleshooted and I have noticed that the error comes from here image

I have tried to import it with Dynamics but still no changes,

image

JayWebtech commented 2 weeks ago

Same here. Were you able to find a solution?

Azania-Mokhampane commented 2 weeks ago

getting the same issue too

Azania-Mokhampane commented 2 weeks ago

@JayWebtech @RamazaniMwemedi this worked for me,

const PayWithPaystack  = () => {

  const isBrowser = typeof window !== 'undefined';

  if (!isBrowser) {
    return null;
  }

  // Import your Paystack-related code here
  const { usePaystackPayment } = require('react-paystack')

const config = {...}

const initializePayment = usePaystackPayment(config);

 return (
    <button onClick={initializePayment}>
      Pay
    </button>
  );
};

export default PayWithPaystack;
stephengade commented 4 days ago

Started getting this error immediately I migrate to Nextjs 15, React 19 (Server component)

RamazaniMwemedi commented 4 days ago

An email from the developer team on how I should go around it.

Here is the link https://paystack.com/docs/api/transaction/#initialize

image

stephengade commented 4 days ago

Hmmm.... That may be straightforward approach. I should have adopted using the API directly rather than SDK.

But currently I fixed the window error by converting the whole page to client with 'use client'

I first of all mark the component with 'use client'

then in /app/payment/page.tsx, I imported the payment component the dynamically disabling the SSR (of course, this will only work by marking the entire page with'use client' as well)

on doing that, the error is gone...