#ManuX
1 messages ยท Page 1 of 1 (latest)
Hi ๐ I'm not familiar with the Next framework, so I'm not too familiar with what could be going on here. I'll leave the thread open though in case anyone in the community has insight to share.
No worries. However you can think about this issue even without considering next.js. Right now loadStripe doesn't seem to be conceived to be passed API keys dynamically right? I think the normal usage would be to pass it a hardcoded ENV variable. So how would you deal in a normal react app if you needed to pass it a dynamic API key that you get from the server?
While keeping it outside of the component that is...
Hi there. Taking over for toby as they have to step out. Let me see if I can find some options
This Stackoverflow post is a little old and references our old React library. However, this approach would still work with the new library: https://stackoverflow.com/a/55520361/17627350
Because the stripe prop accepts a Stripe object too: https://stripe.com/docs/stripe-js/react#elements-provider
Hey! Let me check this out. Thanks a lot for the assistance.
@torn roost
I tried this but it doesn't work :
` const [stripe, setStripe] = React.useState(null);
React.useEffect(() => {
if (typeof window !== undefined) {
if (window.Stripe) {
// this.setState({stripe: window.Stripe('pk_test_12345')});
setStripe(window.Stripe(stripe_account_pk));
} else {
document.querySelector("#stripe-js").addEventListener("load", () => {
// Create Stripe instance once Stripe.js loads
// this.setState({stripe: window.Stripe('pk_test_12345')});
setStripe(window.Stripe(stripe_account_pk));
});
}
}
}, [stripe_account_pk]);
const options = useMemo(() => {
return {
clientSecret,
appearance: {
theme: "stripe",
},
locale,
};
}, [clientSecret, locale]) as StripeElementsOptions;
return (
<Box className="notranslate">
{clientSecret && (
<Elements options={options} stripe={stripe}>
<FormsContainer formsContainerData={formsContainerData} />
</Elements>
)}
</Box>
);
};`
I'm getting:
Unhandled Runtime Error TypeError: Cannot read properties of null (reading 'addEventListener')
Yeah don't follow the docs for react-stripe-elements
That's our old client library
So following anything in https://github.com/stripe-archive/react-stripe-elements#advanced-integrations won't work
You'll need to work on implementation yourself. I just meant a similar approach as outlined in Stackoverflow post (not the react-stripe-elements docs) can be used
So here's actually a better approach I think: https://stackoverflow.com/questions/69388359/how-to-reload-stripe-elements-according-to-different-api-keys
Recommend you try ^
Nice, let me see ๐
Here's some additional good advice from our sdk maintainers: https://github.com/stripe/react-stripe-js/issues/106#issuecomment-653142863. You will have to create a Stripe object for each key, but it's recommended you memoize so as to not keep recreating objects unnecessarily