Skip to content

Commit

Permalink
Throw error when CustomCheckoutContext value is null. (stripe#444)
Browse files Browse the repository at this point in the history
- we don't render any children until the context is set. So as long as
  the app is wrapped in CustomCheckoutProvider, users should not hit
  this error.
- This is to make it easier for TS users to destruct the object without
  worring about nullable cases
  • Loading branch information
pololi-stripe authored Sep 12, 2023
1 parent 15849df commit 17df531
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/components/CustomCheckout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@ export const useElementsOrCustomCheckoutSdkContextWithUseCase = (
return parseElementsContext(elementsContext, useCaseString);
};

export const useCustomCheckout = (): CustomCheckoutContextValue | null => {
export const useCustomCheckout = (): CustomCheckoutContextValue => {
// ensure it's in CustomCheckoutProvider
useCustomCheckoutSdkContextWithUseCase('calls useCustomCheckout()');
return React.useContext(CustomCheckoutContext);
const ctx = React.useContext(CustomCheckoutContext);
if (!ctx) {
throw new Error(
'Could not find CustomCheckout Context; You need to wrap the part of your app that calls useCustomCheckout() in an <CustomCheckoutProvider> provider.'
);
}
return ctx;
};

0 comments on commit 17df531

Please sign in to comment.