Skip to content

Commit

Permalink
Merge pull request #18 from himself65/himself65/hack
Browse files Browse the repository at this point in the history
fix: use React internal variable to improve performance
  • Loading branch information
nanxiaobei committed Jan 15, 2024
2 parents 7aae47e + 4917b27 commit 9286a3b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions 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;

Expand Down Expand Up @@ -109,9 +118,9 @@ const resso = <Obj extends Record<string, unknown>>(obj: Obj): Store<Obj> => {
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];
}
}
Expand Down

0 comments on commit 9286a3b

Please sign in to comment.