We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d410f5e commit fef3676Copy full SHA for fef3676
packages/hooks/src/useGetState/index.ts
@@ -1,5 +1,6 @@
1
import type { Dispatch, SetStateAction } from 'react';
2
-import { useState, useRef, useCallback } from 'react';
+import { useState, useCallback } from 'react';
3
+import useLatest from '../useLatest';
4
5
type GetStateAction<S> = () => S;
6
@@ -13,8 +14,7 @@ function useGetState<S = undefined>(): [
13
14
];
15
function useGetState<S>(initialState?: S) {
16
const [state, setState] = useState(initialState);
- const stateRef = useRef(state);
17
- stateRef.current = state;
+ const stateRef = useLatest(state);
18
19
const getState = useCallback(() => stateRef.current, []);
20
0 commit comments