#colby-react-provider

1 messages · Page 1 of 1 (latest)

kind willowBOT
bronze shoal
#

Hi there

#

Hmm that is strange

#

Can you show me your code where you are calling elements()?

static void
#

React js.

I have it in my index js file
(async () => {
try {
const response = await fetch("/config");
const config = await response.json();
const stripePromise = loadStripe(config.key);

ReactDOM.render(
  <Elements stripe={stripePromise}>
    <App />
  </Elements>,
  document.getElementById("root")
);

} catch (error) {
console.error("Error loading Stripe config:", error);
}
})();

Then in my form itself
<Elements stripe={stripePromise.current} options={{ appearance, clientSecret }}>
<PaymentForm

    />

  </Elements>
bronze shoal
#

What is stripePromise.current?

#

should just be stripePromise, no?

kind willowBOT
static void
#

in my form i am using stripePromise like so
const stripePromise = useRef(null);
Then this is how im calling it

const getConfig = async () => {
const res = await fetch("/config");
const { key } = await res.json();
stripePromise.current = loadStripe(key);
};

bronze shoal
#

Ah okay looks to me like you are rendering Elements multiple times here which is likely the issue

#

You only need to wrap your components in Elements at the top of your tree

static void
#

at the top or root? Cause would root be index.js?

rocky drum
#

colby-react-provider

bronze shoal
#

Yep mostly that is up to you. We recommend rendering your Elements provider at the root so that it is available throughout your whole app. That said, a lot of that will depend on your architecture and the main key is that you render it in a components that wraps wherever your payment form is located.

static void
#

Gotcha, so essentially im rendering to calls to config and its making it act up. So I if I wrapp it in index, Like so
ReactDOM.render(
<Elements stripe={loadStripe(${process.env.PUBLISHABLE_KEY})}>
<React.StrictMode>
<App />
</React.StrictMode>,
</Elements>,
document.getElementById("root")
);
would I still need to call
const getConfig = async () => {
const res = await fetch("/config");
const { key } = await res.json();
stripePromise.current = loadStripe(key);
console.log("stripePromise", stripePromise.current);
};

await getConfig();

since I woudlnt have the promise ready?

rocky drum
#

I'm sorry I know nothing about React and barely understand it and no one else on my team is around. I'm happy to try and help but my advice is going to mostly start with having you try things in Test mode first with clear logs to figure out what's happening

static void
#

No worries, I tried it and its not giving me those issues anymore but ill keep reading thanks