From 324b0dbd2aed2c717d5f1c0bb98570e5da93f1cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Echibur=C3=BA?= Date: Sat, 20 Mar 2021 07:13:05 -0300 Subject: [PATCH] [FEATURE] Passing a function for draft or value for direct state usage (#76) * Enables passing a direct value to the useCallback hook for using it as simple state * Fixed typing error --- src/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index de713f9..95f79fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,14 +12,17 @@ export type ImmerHook = [S, Updater]; export function useImmer( initialValue: S | (() => S) -): [S, (f: (draft: Draft) => void | S) => void]; +): [S, (f: ((draft: Draft | S) => void) | S) => void]; export function useImmer(initialValue: any) { const [val, updateValue] = useState(initialValue); return [ val, useCallback(updater => { - updateValue(produce(updater)); + if(typeof updater === "function") + updateValue(produce(updater)); + else + updateValue(updater); }, []) ]; }