From a1b2d71f8c914966e485aba62fe0ef8cc3839d83 Mon Sep 17 00:00:00 2001
From: Kevin Wolf <hi@kevinwolf.dev>
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/unstated-next.tsx')

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
 	}
 
-- 
cgit v1.2.3

