Skip to content

Commit

Permalink
fix(react): export FeatureHubContext
Browse files Browse the repository at this point in the history
This enables the context to be used with `useContext`. Error handling and default logger assignment must be handled manually in this case.
  • Loading branch information
unstubbable committed Jun 20, 2022
1 parent feb4201 commit b379913
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions packages/react/src/feature-hub-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ export type FeatureHubContextConsumerValue = SomeRequired<
'logger'
>;

const dummyDefaultFeatureHubContextValue = {};

const noFeatureHubContextValueErrorMessage =
'No Feature Hub context was provided! There are two possible causes: 1.) No FeatureHubContextProvider was rendered in the React tree. 2.) A Feature App that renders itself a FeatureAppLoader or a FeatureAppContainer did not declare @feature-hub/react as an external package.';

const FeatureHubContext = React.createContext(
dummyDefaultFeatureHubContextValue as FeatureHubContextProviderValue
);
export const FeatureHubContext = React.createContext<
FeatureHubContextProviderValue | undefined
>(undefined);

/**
* The integrator must render a `FeatureHubContextProvider` to provide the
Expand All @@ -90,7 +88,7 @@ export const FeatureHubContextConsumer = (
) => (
<FeatureHubContext.Consumer>
{(featureHubContextValue) => {
if (featureHubContextValue === dummyDefaultFeatureHubContextValue) {
if (!featureHubContextValue) {
throw new Error(noFeatureHubContextValueErrorMessage);
}

Expand Down

0 comments on commit b379913

Please sign in to comment.