diff options
Diffstat (limited to 'src/unstated-next.tsx')
-rw-r--r-- | src/unstated-next.tsx | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/unstated-next.tsx b/src/unstated-next.tsx index e30d205..5a28ba3 100644 --- a/src/unstated-next.tsx +++ b/src/unstated-next.tsx @@ -13,7 +13,8 @@ export interface Container<Value, State = void> { export function createContainer<Value, State = void>( useHook: (initialState?: State) => Value, ): Container<Value, State> { - let Context = React.createContext<Value | null>(null) + const EMPTY: unique symbol = Symbol() + let Context = React.createContext<Value | typeof EMPTY>(EMPTY) function Provider(props: ContainerProviderProps<State>) { let value = useHook(props.initialState) @@ -22,9 +23,10 @@ export function createContainer<Value, State = void>( function useContainer(): Value { let value = React.useContext(Context) - if (value === null) { + if (value === EMPTY) { throw new Error("Component must be wrapped with <Container.Provider>") } + // @ts-ignore return value } |