aboutsummaryrefslogtreecommitdiff
path: root/src/unstated-next.tsx
diff options
context:
space:
mode:
authorKevin Wolf <hi@kevinwolf.dev>2020-05-04 20:54:08 +0300
committerGitHub <noreply@github.com>2020-05-04 20:54:08 +0300
commita1b2d71f8c914966e485aba62fe0ef8cc3839d83 (patch)
tree916e5332fd3d67bacda75eeb287729efa83184b8 /src/unstated-next.tsx
parent48ca230df908cce3bc32a0672d51426b22e592b8 (diff)
Initialize context with symbol value (#72)
Diffstat (limited to 'src/unstated-next.tsx')
-rw-r--r--src/unstated-next.tsx6
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
}