From a1b2d71f8c914966e485aba62fe0ef8cc3839d83 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Mon, 4 May 2020 11:54:08 -0600 Subject: Initialize context with symbol value (#72) --- src/unstated-next.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') 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 { export function createContainer( useHook: (initialState?: State) => Value, ): Container { - let Context = React.createContext(null) + const EMPTY: unique symbol = Symbol() + let Context = React.createContext(EMPTY) function Provider(props: ContainerProviderProps) { let value = useHook(props.initialState) @@ -22,9 +23,10 @@ export function createContainer( function useContainer(): Value { let value = React.useContext(Context) - if (value === null) { + if (value === EMPTY) { throw new Error("Component must be wrapped with ") } + // @ts-ignore return value } -- cgit v1.2.3