Stripe Elements shows console warnings or errors in a Next.js checkout
Unsupported 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.
Fix it fast
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/null2Match what you see, then jump to its fix
- 1
Unsupported prop change on Elements: You cannot change the `stripe` prop after setting it.loadStripe is called inside a componentFix - 2
Could 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
stripe or elements is null when the submit handler runsstripe and elements are used before Stripe.js has loadedFix - 4
The console shows a CSP violation for js.stripe.com, and the payment form never appearsA Content Security Policy blocks Stripe.jsFix
- 1
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/null2Fix
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 devConfirmed by docs.stripe.com
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/null2Fix
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 devConfirmed by docs.stripe.com
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/null2Fix
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 consoleConfirmed by docs.stripe.com
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-policy2Fix
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.comConfirmed by docs.stripe.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Payments
A payment method is missing from the Stripe Payment Element, or the call to exclude it fails
Excluding apple_pay, google_pay or link with excluded_payment_method_types returns an error
With automatic payment methods, Stripe decides which methods the Payment Element shows.
5 causes, each with a fixPayments
A Stripe webhook to a Vercel function fails with a 308 redirect
HTTP/2 308
Stripe treats every redirect from a webhook URL as a failure and does not follow it.
3 causes, each with a fixPayments
Stripe webhook: no signatures found matching the expected signature
No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
Stripe signs the exact bytes it sends, using that endpoint's own secret.
5 causes, each with a fixPayments
firebase deploy fails because the Stripe client has no API key
Neither apiKey nor config.authenticator provided
The Stripe client is built from a key that is empty at that moment.
3 causes, each with a fixPayments
Metadata you set on a Stripe Checkout Session is empty when you read it
event.data.object.metadata.order_id is None in the webhook handler
Stripe does not copy metadata between objects on its own.
3 causes, each with a fixPayments
Stripe says No such checkout.session when you retrieve a session
resource_missing
A resource_missing error means the ID is not visible to the key you used.
3 causes, each with a fix