mirror of
https://github.com/zhigang1992/react.git
synced 2026-03-26 06:55:07 +08:00
feat(utils): export use-current-state
test(utils): add testcase
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user