-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.ts
44 lines (38 loc) · 1.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import {type MutableRefObject, useMemo} from 'react';
import {useSyncedRef} from '../useSyncedRef/index.js';
export type HookableRefHandler<T> = (v: T) => T;
export function useHookableRef<T>(
initialValue: T,
onSet?: HookableRefHandler<T>,
onGet?: HookableRefHandler<T>
): MutableRefObject<T>;
export function useHookableRef<T = undefined>(): MutableRefObject<T | undefined>;
/**
* Like `React.useRef` but it is possible to define get and set handlers.
*
* @param initialValue Initial value of a hook.
* @param onSet Function to be called while ref.current value set. Return value
* will be stored in ref.
* @param onGet Function to be called while ref.current value accessed. Return
* value will be used as a return value.
*/
export function useHookableRef<T>(
initialValue?: T,
onSet?: HookableRefHandler<T>,
onGet?: HookableRefHandler<T>,
): MutableRefObject<T | undefined> {
const onSetRef = useSyncedRef(onSet);
const onGetRef = useSyncedRef(onGet);
return useMemo(() => {
let v = initialValue;
return {
get current() {
return onGetRef.current === undefined ? v : onGetRef.current(v as T);
},
set current(value) {
v = onSetRef.current === undefined ? value : onSetRef.current(value as T);
},
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}