Skip to content

Commit

Permalink
perf: ⚡️ wrape useGetSetState callbacks in useCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Oct 29, 2018
1 parent dcd1013 commit 3c1e57d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/useGetSetState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useRef} from './react';
import {useRef, useCallback} from './react';
import useUpdate from './useUpdate';

const useGetSetState = <T extends object>(initialState: T = {} as T): [() => T, (patch: Partial<T>) => void]=> {
Expand All @@ -10,8 +10,8 @@ const useGetSetState = <T extends object>(initialState: T = {} as T): [() => T,

const update = useUpdate();
const state = useRef<T>({...(initialState as object)} as T);
const get = () => state.current;
const set = (patch: Partial<T>) => {
const get = useCallback(() => state.current, []);
const set = useCallback((patch: Partial<T>) => {
if (!patch) return;
if (process.env.NODE_ENV !== 'production') {
if (typeof patch !== 'object') {
Expand All @@ -20,7 +20,7 @@ const useGetSetState = <T extends object>(initialState: T = {} as T): [() => T,
}
Object.assign(state.current, patch);
update();
};
}, []);

return [get, set];
};
Expand Down

0 comments on commit 3c1e57d

Please sign in to comment.