diff --git a/src/Immutable.tsx b/src/Immutable.tsx index 6ed958e..0a76d38 100644 --- a/src/Immutable.tsx +++ b/src/Immutable.tsx @@ -1,7 +1,16 @@ import { supportRef } from 'rc-util/lib/ref'; import * as React from 'react'; -const RenderContext = React.createContext(0); +const ImmutableContext = React.createContext(0); + +/** + * Get render update mark by `makeImmutable` root. + * Do not deps on the return value as render times + * but only use for `useMemo` or `useCallback` deps. + */ +export function useImmutableMark() { + return React.useContext(ImmutableContext); +} /** * Wrapped Component will be marked as Immutable. @@ -17,9 +26,9 @@ export function makeImmutable>(Component: T): renderTimesRef.current += 1; return ( - + - + ); }; @@ -45,7 +54,7 @@ export function responseImmutable>( const ImmutableComponent = function (props: any, ref: any) { const refProps = refAble ? { ref } : {}; - React.useContext(RenderContext); + useImmutableMark(); return ; }; diff --git a/src/index.ts b/src/index.ts index 0adb1aa..4cd3331 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import type { SelectorContext } from './context'; import { createContext, useContext } from './context'; -import { makeImmutable, responseImmutable } from './Immutable'; +import { makeImmutable, responseImmutable, useImmutableMark } from './Immutable'; -export { createContext, useContext, makeImmutable, responseImmutable }; +export { createContext, useContext, makeImmutable, responseImmutable, useImmutableMark }; export type { SelectorContext };