31 lines
597 B
TypeScript
31 lines
597 B
TypeScript
import {
|
|
Dispatch,
|
|
MutableRefObject,
|
|
SetStateAction,
|
|
useEffect,
|
|
useRef,
|
|
useState,
|
|
} from 'react'
|
|
|
|
export function useRefState<S>(
|
|
initialState: S | (() => S)
|
|
): [S, Dispatch<SetStateAction<S>>, MutableRefObject<S>] {
|
|
const [state, setState] = useState<S>(initialState)
|
|
const stateRef = useRef(state)
|
|
useEffect(() => {
|
|
stateRef.current = state
|
|
}, [state])
|
|
return [state, setState, stateRef]
|
|
}
|
|
|
|
export function useTimer() {}
|
|
|
|
export function useValueRef<V>(value: V) {
|
|
const ref = useRef(value)
|
|
useEffect(() => {
|
|
ref.current = value
|
|
}, [value])
|
|
|
|
return ref
|
|
}
|