Skip to content

Commit fef3676

Browse files
coding-iceWait
authored and
Wait
committed
refactor: stateRef to useLatest (alibaba#2407)
1 parent d410f5e commit fef3676

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

packages/hooks/src/useGetState/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Dispatch, SetStateAction } from 'react';
2-
import { useState, useRef, useCallback } from 'react';
2+
import { useState, useCallback } from 'react';
3+
import useLatest from '../useLatest';
34

45
type GetStateAction<S> = () => S;
56

@@ -13,8 +14,7 @@ function useGetState<S = undefined>(): [
1314
];
1415
function useGetState<S>(initialState?: S) {
1516
const [state, setState] = useState(initialState);
16-
const stateRef = useRef(state);
17-
stateRef.current = state;
17+
const stateRef = useLatest(state);
1818

1919
const getState = useCallback(() => stateRef.current, []);
2020

0 commit comments

Comments
 (0)