Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Payments and webhooks

Stripe Elements shows console warnings or errors in a Next.js checkout

ErrorUnsupported prop change on Elements: You cannot change the `stripe` prop after setting it.

Short answer

Most React Stripe.js warnings come from four mistakes: calling loadStripe inside a component, using useStripe or useElements outside the Elements provider, using the objects before they have loaded, and a Content Security Policy that blocks js.stripe.com. Match the message to the cause below.

  • Applies to Next.js 15, Next.js 16, React 19, @stripe/react-stripe-js, @stripe/stripe-js
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place the app creates the Stripe object and every place it renders Elements.

    grep -rn "loadStripe\|<Elements\|useElements\|useStripe" app components src 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1Unsupported prop change on Elements: You cannot change the `stripe` prop after setting it.loadStripe is called inside a componentFix
    2. 2Could not find Elements context; You need to wrap the part of your app that calls useElements() in an <Elements> provider.useElements or useStripe runs outside an Elements providerFix
    3. 3stripe or elements is null when the submit handler runsstripe and elements are used before Stripe.js has loadedFix
    4. 4The console shows a CSP violation for js.stripe.com, and the payment form never appearsA Content Security Policy blocks Stripe.jsFix

What does the error look like?

  • Unsupported prop change on Elements: You cannot change the `stripe` prop after setting it.
  • Could not find Elements context; You need to wrap the part of your app that calls useElements() in an <Elements> provider.

Cause 1 of 4

loadStripe is called inside a component

1Check

The React Stripe.js reference says the stripe prop cannot change once set, and its example comment says to call loadStripe outside a component's render so the Stripe object is not recreated on every render. A loadStripe call in the component body gives Elements a new promise each time.

grep -rn "loadStripe(" app components src 2>/dev/null

2Fix

Create the promise once at module level and pass that same value to Elements.

'use client'
import { Elements } from '@stripe/react-stripe-js'
import { loadStripe } from '@stripe/stripe-js'

const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!)

export default function Checkout({ clientSecret }: { clientSecret: string }) {
  return (
    <Elements stripe={stripePromise} options={{ clientSecret }}>
      {/* form */}
    </Elements>
  )
}

3Verify

Reload the checkout page and confirm the warning is gone from the console.

npm run dev

Confirmed by docs.stripe.com

Did this fix it?

Cause 2 of 4

useElements or useStripe runs outside an Elements provider

1Check

The error is thrown when the hook or ElementsConsumer has no Elements parent. The provider must wrap the component that calls the hook, not sit next to it.

grep -rn "useElements\|useStripe" app components src 2>/dev/null

2Fix

Render the Elements provider in a parent component and put the form that calls the hooks inside it.

<Elements stripe={stripePromise} options={options}>
  <CheckoutForm />  {/* useStripe() and useElements() live here */}
</Elements>

3Verify

Open the checkout page; the page renders the payment form with no thrown error.

npm run dev

Confirmed by docs.stripe.com

Did this fix it?

Cause 3 of 4

stripe and elements are used before Stripe.js has loaded

1Check

The reference says that if the Promise passed to Elements has not resolved yet, useStripe and useElements return null.

grep -rn "confirmPayment" app components src 2>/dev/null

2Fix

Return early from the handler while either is null, and disable the submit button until stripe exists.

const stripe = useStripe()
const elements = useElements()

async function handleSubmit(e: React.FormEvent) {
  e.preventDefault()
  if (!stripe || !elements) return
  // await stripe.confirmPayment({ elements, confirmParams: { return_url } })
}

// <button disabled={!stripe}>Pay</button>

3Verify

Click Pay before the form finishes loading; nothing throws.

# no TypeError about null in the console

Confirmed by docs.stripe.com

Did this fix it?

Cause 4 of 4

A Content Security Policy blocks Stripe.js

1Check

If you send a Content-Security-Policy header, it must allow Stripe's domains. Look at the header your app returns.

curl -sI https://example.com | grep -i content-security-policy

2Fix

Add the directives Stripe lists for Stripe.js to your policy.

connect-src https://api.stripe.com https://maps.googleapis.com;
frame-src https://*.js.stripe.com https://js.stripe.com https://hooks.stripe.com;
script-src https://*.js.stripe.com https://js.stripe.com https://maps.googleapis.com;

3Verify

Reload with the console open and confirm there is no violation for js.stripe.com.

# no Content Security Policy errors mentioning stripe.com

Confirmed by docs.stripe.com

Did this fix it?

How do I stop it coming back?

  • Keep the loadStripe call at module level in one file and import that promise where needed.
  • Mount one Elements provider per checkout, with the payment form inside it.
  • Disable the pay button until useStripe returns a value.
  • Use test keys until you go live, and take the live publishable key from environment variables.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.