diff options
author | Jamie Kyle <me@thejameskyle.com> | 2019-05-01 22:17:21 +0300 |
---|---|---|
committer | Jamie Kyle <me@thejameskyle.com> | 2019-05-02 03:45:10 +0300 |
commit | 436fe821b81675f1aa6d03c30dc3a3604f8d2781 (patch) | |
tree | bc750c1728f90ef75f4b266c25b3ee6707c1bb18 /src |
init commit
Diffstat (limited to 'src')
-rw-r--r-- | src/unstated-next.tsx | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/unstated-next.tsx b/src/unstated-next.tsx new file mode 100644 index 0000000..b19f8ba --- /dev/null +++ b/src/unstated-next.tsx @@ -0,0 +1,33 @@ +import React from "react" + +interface ContainerProviderProps { + children: React.ReactNode +} + +interface Container<Value> { + Provider: React.ComponentType<ContainerProviderProps> + useContainer: () => Value +} + +export function createContainer<Value>(useHook: () => Value): Container<Value> { + let Context = React.createContext<Value | null>(null) + + function Provider(props: ContainerProviderProps) { + let value = useHook() + return <Context.Provider value={value}>{props.children}</Context.Provider> + } + + function useContainer(): Value { + let value = React.useContext(Context) + if (value === null) { + throw new Error("Component must be wrapped with <Container.Provider>") + } + return value + } + + return { Provider, useContainer } +} + +export function useContainer<Value>(container: Container<Value>): Value { + return container.useContainer() +} |