Skip to content

Commit ce553f6

Browse files
committed
feat: export useImmutableMark
1 parent 71e2b15 commit ce553f6

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Immutable.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { supportRef } from 'rc-util/lib/ref';
22
import * as React from 'react';
33

4-
const RenderContext = React.createContext<number>(0);
4+
const ImmutableContext = React.createContext<number>(0);
5+
6+
/**
7+
* Get render update mark by `makeImmutable` root.
8+
* Do not deps on the return value as render times
9+
* but only use for `useMemo` or `useCallback` deps.
10+
*/
11+
export function useImmutableMark() {
12+
return React.useContext(ImmutableContext);
13+
}
514

615
/**
716
* Wrapped Component will be marked as Immutable.
@@ -17,9 +26,9 @@ export function makeImmutable<T extends React.ComponentType<any>>(Component: T):
1726
renderTimesRef.current += 1;
1827

1928
return (
20-
<RenderContext.Provider value={renderTimesRef.current}>
29+
<ImmutableContext.Provider value={renderTimesRef.current}>
2130
<Component {...props} {...refProps} />
22-
</RenderContext.Provider>
31+
</ImmutableContext.Provider>
2332
);
2433
};
2534

@@ -45,7 +54,7 @@ export function responseImmutable<T extends React.ComponentType<any>>(
4554

4655
const ImmutableComponent = function (props: any, ref: any) {
4756
const refProps = refAble ? { ref } : {};
48-
React.useContext(RenderContext);
57+
useImmutableMark();
4958

5059
return <Component {...props} {...refProps} />;
5160
};

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SelectorContext } from './context';
22
import { createContext, useContext } from './context';
3-
import { makeImmutable, responseImmutable } from './Immutable';
3+
import { makeImmutable, responseImmutable, useImmutableMark } from './Immutable';
44

5-
export { createContext, useContext, makeImmutable, responseImmutable };
5+
export { createContext, useContext, makeImmutable, responseImmutable, useImmutableMark };
66
export type { SelectorContext };

0 commit comments

Comments
 (0)