#ricardo-react-elements
1 messages · Page 1 of 1 (latest)
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
What is the second body here? Is it within the <Elements> tag?
BodyComponent changes depending on the step
Something like:
const useBody = ({ step }) => {
if (step === 1 ) return Body1;
if (step === 2 ) return Body2;
}
Okay so you are re-rendering the Body component and in Step 2 it does not recognize the Elements context it is loaded in?
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...
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.
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
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>
ricardo-react-elements
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.
Can you give us a clear end to end reproduction so that we can look at the code and what happens?
I'm going to try and reproduce this in codesandbox
When reproducing, can you also try rendering the <Elements> provider component at the root of the application?
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
this is the closest to live support you can get. We won't do a call or screensharing.