From 12a8a2c098edb721bbbf754df2ea2503bee916a8 Mon Sep 17 00:00:00 2001 From: gideon-shore Date: Sun, 2 Jan 2022 20:10:42 +0800 Subject: [PATCH 1/3] feat: Fix Type Error When useHook Function needs any required arguments --- src/unstated-next.tsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/unstated-next.tsx b/src/unstated-next.tsx index 40b1a25..c352002 100644 --- a/src/unstated-next.tsx +++ b/src/unstated-next.tsx @@ -2,17 +2,18 @@ import React from "react" const EMPTY: unique symbol = Symbol() -export interface ContainerProviderProps { +export type ContainerProviderProps< + State extends any +> = React.PropsWithChildren<{ initialState?: State - children: React.ReactNode -} +}> -export interface Container { +export interface Container { Provider: React.ComponentType> useContainer: () => Value } -export function createContainer( +export function createContainer( useHook: (initialState?: State) => Value, ): Container { let Context = React.createContext(EMPTY) @@ -33,7 +34,7 @@ export function createContainer( return { Provider, useContainer } } -export function useContainer( +export function useContainer( container: Container, ): Value { return container.useContainer() From a84bff394fa1ca079769804673a6d48114b75c2b Mon Sep 17 00:00:00 2001 From: gideon-shore Date: Sun, 2 Jan 2022 20:57:35 +0800 Subject: [PATCH 2/3] feat: Fix Type Error When useHook Function needs any required arguments --- example/index.tsx | 34 +++++++++++++++++++++++++++------- src/unstated-next.tsx | 20 +++++++++++--------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/example/index.tsx b/example/index.tsx index 566ebaf..af7dd23 100644 --- a/example/index.tsx +++ b/example/index.tsx @@ -3,16 +3,24 @@ import { createContainer } from "../src/unstated-next" import { render } from "react-dom" function useCounter(initialState = 0) { - let [count, setCount] = useState(initialState) - let decrement = () => setCount(count - 1) - let increment = () => setCount(count + 1) + const [count, setCount] = useState(initialState) + const decrement = () => setCount(count - 1) + const increment = () => setCount(count + 1) return { count, decrement, increment } } -let Counter = createContainer(useCounter) +const Counter = createContainer(useCounter) + +function useRequiredCounter(step: number) { + const { count } = Counter.useContainer() + const computed = count + step + return { count, step, computed } +} + +const RequiredCounter = createContainer(useRequiredCounter) function CounterDisplay() { - let counter = Counter.useContainer() + const counter = Counter.useContainer() return (
@@ -22,14 +30,26 @@ function CounterDisplay() { ) } +function RequiredCounterDisplay() { + const { count, step, computed } = RequiredCounter.useContainer() + return ( +
+ Computed Value With Step {step}: {count} + {step} = {computed} +
+ ) +} + function App() { return ( - + - +
+ + +
diff --git a/src/unstated-next.tsx b/src/unstated-next.tsx index c352002..9d4b026 100644 --- a/src/unstated-next.tsx +++ b/src/unstated-next.tsx @@ -3,24 +3,26 @@ import React from "react" const EMPTY: unique symbol = Symbol() export type ContainerProviderProps< - State extends any + State extends any[] > = React.PropsWithChildren<{ - initialState?: State + initialState: State }> -export interface Container { +export type UseHookFn = (...args: State) => Value + +export interface Container { Provider: React.ComponentType> useContainer: () => Value } -export function createContainer( - useHook: (initialState?: State) => Value, +export function createContainer( + useHook: UseHookFn, ): Container { let Context = React.createContext(EMPTY) - function Provider(props: ContainerProviderProps) { - let value = useHook(props.initialState) - return {props.children} + function Provider({ initialState, children }: ContainerProviderProps) { + const value = useHook(...initialState) + return {children} } function useContainer(): Value { @@ -34,7 +36,7 @@ export function createContainer( return { Provider, useContainer } } -export function useContainer( +export function useContainer( container: Container, ): Value { return container.useContainer() From 75f286a3ccf7e776e2c61baf06ec754eb99f7c3a Mon Sep 17 00:00:00 2001 From: gideon-shore Date: Sun, 2 Jan 2022 21:08:04 +0800 Subject: [PATCH 3/3] feat: Version Changes --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b186f75..bbd415e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unstated-next", - "version": "1.1.0", + "version": "2.0.0", "description": "200 bytes to never think about React state management libraries ever again", "source": "src/unstated-next.tsx", "main": "dist/unstated-next.js",