Skip to content

Commit 26c8a16

Browse files
committed
named importing hooks
1 parent b88b623 commit 26c8a16

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-ts-ignore */
22

3-
import React from 'react';
3+
import React, {
4+
createElement,
5+
createContext as createContextOrig,
6+
useCallback,
7+
useContext as useContextOrig,
8+
useMemo,
9+
useRef,
10+
} from 'react';
411

5-
const createMutableSource = (React as any).createMutableSource as any;
6-
const useMutableSource = (React as any).useMutableSource as any;
12+
const createMutableSource = 'NOT_AVAILABLE_YET' as any;
13+
const useMutableSource = 'NOT_AVAILABLE_YET' as any;
714

815
const SOURCE_SYMBOL = Symbol();
916

@@ -12,16 +19,16 @@ type ContextValue<Value> = {
1219
[SOURCE_SYMBOL]: any;
1320
};
1421

15-
const createProvider = <Value>(OrigProvider: React.Provider<ContextValue<Value>>) => {
22+
const createProvider = <Value>(ProviderOrig: React.Provider<ContextValue<Value>>) => {
1623
const Provider: React.FC<{ value: Value }> = ({ value, children }) => {
17-
const ref = React.useRef({ value, listeners: new Set<() => void>() });
24+
const ref = useRef({ value, listeners: new Set<() => void>() });
1825
ref.current.value = value;
1926
ref.current.listeners.forEach((listener) => listener());
20-
const contextValue = React.useMemo(() => {
27+
const contextValue = useMemo(() => {
2128
const source = createMutableSource(ref, () => ref.current.value);
2229
return { [SOURCE_SYMBOL]: source };
2330
}, []);
24-
return React.createElement(OrigProvider, { value: contextValue }, children);
31+
return createElement(ProviderOrig, { value: contextValue }, children);
2532
};
2633
return React.memo(Provider);
2734
};
@@ -40,7 +47,7 @@ export const createContext = <Value>(defaultValue: Value) => {
4047
const source = createMutableSource({ current: defaultValue }, {
4148
getVersion: () => defaultValue,
4249
});
43-
const context = React.createContext(
50+
const context = createContextOrig(
4451
{ [SOURCE_SYMBOL]: source },
4552
) as unknown as React.Context<Value>; // HACK typing
4653
context.Provider = createProvider(
@@ -68,16 +75,16 @@ export const useContextSelector = <Value, Selected>(
6875
context: React.Context<Value>,
6976
selector: (value: Value) => Selected,
7077
) => {
71-
const { [SOURCE_SYMBOL]: source } = React.useContext(
78+
const { [SOURCE_SYMBOL]: source } = useContextOrig(
7279
context,
7380
) as unknown as ContextValue<Value>; // HACK typing
7481
if (!source) {
7582
throw new Error('useContextSelector requires special context');
7683
}
77-
const getSnapshot = React.useCallback((
84+
const getSnapshot = useCallback((
7885
ref: React.MutableRefObject<{ value: Value }>,
7986
) => selector(ref.current.value), [selector]);
80-
const subscribe = React.useCallback((
87+
const subscribe = useCallback((
8188
ref: React.MutableRefObject<{ value: Value; listeners: Set<() => void> }>,
8289
callback: () => void,
8390
) => {

0 commit comments

Comments
 (0)