From 4917b27d47161a335d2a2f10318e58019fb2f71c Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Sun, 14 Jan 2024 20:53:47 -0600 Subject: [PATCH] fix: use React internal variable to improve performance --- src/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 195188f..0e257fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,14 @@ +import { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } from 'react' import { useSyncExternalStore } from 'use-sync-external-store/shim'; +declare module 'react' { + var __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: { + ReactCurrentOwner: { + current: null | unknown; + }; + } +} + type VoidFn = () => void; type AnyFn = (...args: unknown[]) => unknown; @@ -109,9 +118,9 @@ const resso = >(obj: Obj): Store => { return obj[key]; } - try { + if (__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner.current !== null) { return state[key].useSnapshot(); - } catch (err) { + } else { return obj[key]; } }