From f75e4529109640e39e527cdadda66d5e748ab1bf Mon Sep 17 00:00:00 2001
From: Jamie Kyle <me@thejameskyle.com>
Date: Mon, 13 May 2019 16:31:32 -0700
Subject: add initialState

---
 src/unstated-next.tsx | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

(limited to 'src/unstated-next.tsx')

diff --git a/src/unstated-next.tsx b/src/unstated-next.tsx
index 94e9c79..e30d205 100644
--- a/src/unstated-next.tsx
+++ b/src/unstated-next.tsx
@@ -1,19 +1,22 @@
 import React from "react"
 
-export interface ContainerProviderProps {
+export interface ContainerProviderProps<State = void> {
+	initialState?: State
 	children: React.ReactNode
 }
 
-export interface Container<Value> {
-	Provider: React.ComponentType<ContainerProviderProps>
+export interface Container<Value, State = void> {
+	Provider: React.ComponentType<ContainerProviderProps<State>>
 	useContainer: () => Value
 }
 
-export function createContainer<Value>(useHook: () => Value): Container<Value> {
+export function createContainer<Value, State = void>(
+	useHook: (initialState?: State) => Value,
+): Container<Value, State> {
 	let Context = React.createContext<Value | null>(null)
 
-	function Provider(props: ContainerProviderProps) {
-		let value = useHook()
+	function Provider(props: ContainerProviderProps<State>) {
+		let value = useHook(props.initialState)
 		return <Context.Provider value={value}>{props.children}</Context.Provider>
 	}
 
@@ -28,6 +31,8 @@ export function createContainer<Value>(useHook: () => Value): Container<Value> {
 	return { Provider, useContainer }
 }
 
-export function useContainer<Value>(container: Container<Value>): Value {
+export function useContainer<Value, State = void>(
+	container: Container<Value, State>,
+): Value {
 	return container.useContainer()
 }
-- 
cgit v1.2.3

