diff options
author | Jamie Kyle <me@thejameskyle.com> | 2019-05-14 02:31:32 +0300 |
---|---|---|
committer | Jamie Kyle <me@thejameskyle.com> | 2019-05-14 02:31:32 +0300 |
commit | f75e4529109640e39e527cdadda66d5e748ab1bf (patch) | |
tree | 0edacda1d2ad832e034a86d5c47c540d37c43f0d /src/unstated-next.tsx | |
parent | badab5b6aba05a76abeb5e87fc4c531fd1a04d68 (diff) |
add initialState
Diffstat (limited to 'src/unstated-next.tsx')
-rw-r--r-- | src/unstated-next.tsx | 19 |
1 files changed, 12 insertions, 7 deletions
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() } |