Skip to content

Commit

Permalink
refactor: stateRef to useLatest (alibaba#2407)
Browse files Browse the repository at this point in the history
  • Loading branch information
coding-ice authored and raotaohub committed Mar 28, 2024
1 parent 15675cb commit 935b8b4
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/hooks/src/useGetState/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Dispatch, SetStateAction } from 'react';
import { useState, useRef, useCallback } from 'react';
import { useState, useCallback } from 'react';
import useLatest from '../useLatest';

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

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

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

Expand Down

0 comments on commit 935b8b4

Please sign in to comment.