From f75e4529109640e39e527cdadda66d5e748ab1bf Mon Sep 17 00:00:00 2001 From: Jamie Kyle 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') 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 { + initialState?: State children: React.ReactNode } -export interface Container { - Provider: React.ComponentType +export interface Container { + Provider: React.ComponentType> useContainer: () => Value } -export function createContainer(useHook: () => Value): Container { +export function createContainer( + useHook: (initialState?: State) => Value, +): Container { let Context = React.createContext(null) - function Provider(props: ContainerProviderProps) { - let value = useHook() + function Provider(props: ContainerProviderProps) { + let value = useHook(props.initialState) return {props.children} } @@ -28,6 +31,8 @@ export function createContainer(useHook: () => Value): Container { return { Provider, useContainer } } -export function useContainer(container: Container): Value { +export function useContainer( + container: Container, +): Value { return container.useContainer() } -- cgit v1.2.3