Skip to content

Commit 038b018

Browse files
committed
added useRefWithState
1 parent b06443f commit 038b018

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/helpers/hooks.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,14 @@ export function useStateEX<ValueType>(initialValue: ValueType, options?: {
104104
}), []);
105105

106106
return [value, setValue, currentValue];
107+
}
108+
109+
export function useRefWithState<T>(initialValue?: T) {
110+
let asRef = useRef<T>(initialValue);
111+
let [asState, setState] = useState<T>(initialValue);
112+
let setRef = useCallback((newValue: T) => {
113+
asRef.current = newValue;
114+
setState(newValue);
115+
}, useEffectOnlyOnMount);
116+
return { asRef, asState, setRef };
107117
}

0 commit comments

Comments
 (0)