react-paystack icon indicating copy to clipboard operation
react-paystack copied to clipboard

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

Open RamazaniMwemedi opened this issue 1 year ago • 9 comments

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

RamazaniMwemedi avatar Oct 08 '24 00:10 RamazaniMwemedi

Same here. Were you able to find a solution?

JayWebtech avatar Oct 31 '24 10:10 JayWebtech

getting the same issue too

Azania-Mokhampane avatar Oct 31 '24 22:10 Azania-Mokhampane

@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;

Azania-Mokhampane avatar Oct 31 '24 22:10 Azania-Mokhampane

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

stephengade avatar Nov 12 '24 21:11 stephengade

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

RamazaniMwemedi avatar Nov 13 '24 09:11 RamazaniMwemedi

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...

stephengade avatar Nov 13 '24 11:11 stephengade

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)

@stephengade 's comment did the trick

My implementation looks like this, using the next/dynamic utility to import the PaystackButton

- import { PaystackButton } from "react-paystack";

+ import dynamic from 'next/dynamic';
+ const PaystackButton = dynamic(() => import("react-paystack").then((c) => c.PaystackButton), {
+   ssr: false,
+ });

chizidotdev avatar Dec 24 '24 11:12 chizidotdev

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)

@stephengade 's comment did the trick

My implementation looks like this, using the next/dynamic utility to import the PaystackButton

- import { PaystackButton } from "react-paystack";

+ import dynamic from 'next/dynamic';
+ const PaystackButton = dynamic(() => import("react-paystack").then((c) => c.PaystackButton), {
+   ssr: false,
+ });

this seems to work. thanks

osfieldgaga avatar Jan 01 '25 10:01 osfieldgaga

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)

@stephengade 's comment did the trick

My implementation looks like this, using the next/dynamic utility to import the PaystackButton

  • import { PaystackButton } from "react-paystack";
  • import dynamic from 'next/dynamic';
  • const PaystackButton = dynamic(() => import("react-paystack").then((c) => c.PaystackButton), {
  • ssr: false,
  • });

This works great

0necontroller avatar Jan 19 '25 11:01 0necontroller

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 ); };

export default PayWithPaystack;

This approach helped, but with linting you can't call a hook conditionally To avoid linting errors, wrap this component in a custom ClientOnly component (remove the isBrowser conditional return)

Wrapper ClientOnly component: [isClient, setIsClient] = useState(false) if (isClient) return null return {children}

Usage: <ClientOnly><PaystackButton /></ClientOnly>

Kasopej avatar Aug 22 '25 10:08 Kasopej

Each time I initialize my paynent from my NextJs App, I get a response like this { "status": "fail", "message": "Failed to initialize Paystack transaction: Invalid URL" }

Please what should I do

CEN-Smart avatar Oct 04 '25 03:10 CEN-Smart

Hard to know without context, but I think you need to check you have correct callback URL configured on paystack dashboard

Looks like an issue with your paystack url configuration on the paystack admin dashboard or your backend api

Kasopej avatar Oct 04 '25 09:10 Kasopej

Image This is my callback_url configured on my dashboard, I even added it as a payload to be sent from the frontend, yet after successful transaction, It is not redirected to it, Or can't it work on test mode?

CEN-Smart avatar Oct 04 '25 11:10 CEN-Smart