diff --git a/src/useGetSetState.ts b/src/useGetSetState.ts index bc982c0f9b..453d6cc5e6 100644 --- a/src/useGetSetState.ts +++ b/src/useGetSetState.ts @@ -1,4 +1,4 @@ -import {useRef} from './react'; +import {useRef, useCallback} from './react'; import useUpdate from './useUpdate'; const useGetSetState = (initialState: T = {} as T): [() => T, (patch: Partial) => void]=> { @@ -10,8 +10,8 @@ const useGetSetState = (initialState: T = {} as T): [() => T, const update = useUpdate(); const state = useRef({...(initialState as object)} as T); - const get = () => state.current; - const set = (patch: Partial) => { + const get = useCallback(() => state.current, []); + const set = useCallback((patch: Partial) => { if (!patch) return; if (process.env.NODE_ENV !== 'production') { if (typeof patch !== 'object') { @@ -20,7 +20,7 @@ const useGetSetState = (initialState: T = {} as T): [() => T, } Object.assign(state.current, patch); update(); - }; + }, []); return [get, set]; };