#ricardo-react-elements

1 messages · Page 1 of 1 (latest)

lapis yewBOT
little lance
#

BodyComponent is a hook that renders the right Body depending on the modal step

#

When rendering the second body (step 2) that uses useStripe it throws the error, saying it cant find Elements Context

wild bough
#

What is the second body here? Is it within the <Elements> tag?

little lance
#

BodyComponent changes depending on the step

#

Something like:

const useBody = ({ step }) => {
if (step === 1 ) return Body1;
if (step === 2 ) return Body2;
}
wild bough
#

Okay so you are re-rendering the Body component and in Step 2 it does not recognize the Elements context it is loaded in?

little lance
#

Exactly...

#

In reality, when I change the step, I'm re-rendering everything in that screenshot. I Should only re-render the BodyComponent, but yeah...

wild bough
#

Yeah, this will likely break the required connection between the <Elements> component and the components rendered within it. I think you will need to render the complete checkout flow in a single component and perhaps use show/hide logic to achieve the effect you are going for.

lapis yewBOT
little lance
#

That Body2 that uses useStripe() is in a Shared folder. If I copy paste it to near this Modal, it actually works.

#

The problem is that other components also use this Body2 and doing this change, will introduce a lot of duplicated code

wild bough
#

It sounds like you are recreating a new Elements component instance when you transition to <Body2>. This will mean it looses all context of what happened in <BodyComponent>

patent granite
#

ricardo-react-elements

little lance
#

I don't think that is the problem. If I put the <Elements> element in _app.tsx (which is the file executed by nextJS when a pages load) it gives the same error. I don't think the problem is because of useState or re-render.

patent granite
#

Can you give us a clear end to end reproduction so that we can look at the code and what happens?

little lance
#

I'm going to try and reproduce this in codesandbox

wild bough
#

When reproducing, can you also try rendering the <Elements> provider component at the root of the application?

little lance
#

I can't reproduce this error in a new application. Same logic but its working on the new app

#

Is there a way to get live support? I can't share the code/repo

patent granite
#

this is the closest to live support you can get. We won't do a call or screensharing.