From 764e465842c003c9b3b8a3b5b464637e8ebe77e5 Mon Sep 17 00:00:00 2001 From: Yair Even Or Date: Thu, 9 Oct 2025 11:36:12 +0300 Subject: [PATCH] fixes #143 - move invalid react hook call to be called at top-level https://react.dev/reference/rules/rules-of-hooks#only-call-hooks-at-the-top-level --- src/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index a729473..e63bdce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,13 +14,13 @@ export function useImmer(initialValue: any) { true ) ); - return [ - val, - useCallback((updater) => { - if (typeof updater === "function") updateValue(produce(updater)); - else updateValue(freeze(updater)); - }, []), - ]; + + const setter = useCallback((updater) => { + if (typeof updater === "function") updateValue(produce(updater)); + else updateValue(freeze(updater)); + }, []), + + return [val, setter]; } // Provides different overloads of `useImmerReducer` similar to `useReducer` from `@types/react`.