From 3c1e57d60e030773ec30aa98e7e72132656a053f Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 29 Oct 2018 17:46:28 +0100 Subject: [PATCH] =?UTF-8?q?perf:=20=E2=9A=A1=EF=B8=8F=20wrape=20useGetSetS?= =?UTF-8?q?tate=20callbacks=20in=20useCallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/useGetSetState.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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]; };