Open frederichoule opened 2 months ago
By forcing Elements.svelte
to re-set the context when the variables are updated, it seems to work.
$: setContext('stripe', { stripe, elements });
Tentative PR here: https://github.com/joshnuss/svelte-stripe/pull/123
Hey @joshnuss did you have any chance to look into this, or find a better fix? Thanks
Hi @frederichoule!
I'm wondering, what's different in your repo vs this one? Did you change anything?
Because when I try it with the live Svelte PaymentElement example it seems to work OK
Did you try my repo? There's a new button in the Svelte PaymentElement example page that says "Hide/Show" and replicates the bug.
The only change to the actual payment element is there: https://github.com/joshnuss/svelte-stripe/pull/123/commits/254b636b4fbd351b64e74819ae475d39c43095a0
Line 71: $: setContext('stripe', { stripe, elements });
Hey @joshnuss, the example wasn't in the repo, sorry it's my fault. It is now.
Example: https://github.com/frederichoule/svelte-stripe Fix: https://github.com/joshnuss/svelte-stripe/pull/123
Any update @joshnuss ?
I am getting the same error when I use Elements in a multi-step form where the user advances to the payment element step, but goes back and then again to the step with the payment element, then an error is thrown and the app stops working:
Unhandled Promise Rejection: TypeError: Right side of assignment cannot be destructured
The error stems from calling getContext in an element that returns an undefined value.
I rectified the bug by simply adding to the Elements.svelte a onDestroy lifecycle function. When unmounting the Elements component, the elements objects and its contents gets garbage collected.
onDestroy(() => { if (elements) { elements = null; } });
Getting ready for production in the next few days, and no update from @joshnuss - so here's what I did to fix it without forking the whole library:
<script lang="ts">
let stripeElements: StripeElements | null = $state(null);
const svelteStripeFix = (e: HTMLDivElement) => {
$effect(() => {
return () => {
stripeElements = null;
};
});
};
</script>
<div use:svelteStripeFix>
<Elements bind:elements={stripeElements}><!-- Elements here //--></Elements>
</div>
Basically, as soon as the div surrounding the Elements is destroyed, we just clear the binded reference to it and then everything works again the next time you mount it in the same payment flow.
Thanks @TheRealThor for pointing me in the right direction!
@frederichoule apologies for the delay. I will take a look this week.
Describe the bug
When creating, destroying and recreating the Payment Element, we get an error related to getContext() in Payment Elements.
Reproduction
https://localhost:5174/examples/payment-element
show/hide
buttonSeverity
blocking all usage
Additional Information
No response