feat(utils): export use-current-state

test(utils): add testcase
This commit is contained in:
unix
2020-05-01 18:43:42 +08:00
parent 6cfc87e917
commit 16c879ab3e
4 changed files with 62 additions and 21 deletions

View File

@@ -1,23 +1,3 @@
import { Dispatch, MutableRefObject, SetStateAction, useEffect, useRef, useState } from 'react'
export type CurrentStateType<S> = [
S, Dispatch<SetStateAction<S>>, MutableRefObject<S>
]
const useCurrentState = <S,>(initialState: S): CurrentStateType<S> => {
const [state, setState] = useState<S>(initialState as S)
const ref = useRef<S>(initialState as S)
useEffect(() => {
ref.current = state
}, [state])
const setValue = (val: S) => {
ref.current = val
setState(val)
}
return [state, setValue, ref]
}
import useCurrentState from '../utils-shared/use-current-state'
export default useCurrentState