From 16f023fc666ad674d3dba89206230acbbf974735 Mon Sep 17 00:00:00 2001 From: Tyler Swavely Date: Fri, 23 Aug 2019 00:02:35 -0700 Subject: [PATCH] fix(useSetState): memoize setState callback --- src/useSetState.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/useSetState.ts b/src/useSetState.ts index d09004390e..ba40da2139 100644 --- a/src/useSetState.ts +++ b/src/useSetState.ts @@ -1,12 +1,15 @@ -import { useState } from 'react'; +import { useState, useCallback } from 'react'; const useSetState = ( initialState: T = {} as T ): [T, (patch: Partial | ((prevState: T) => Partial)) => void] => { const [state, set] = useState(initialState); - const setState = patch => { - set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch)); - }; + const setState = useCallback( + patch => { + set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch)); + }, + [set] + ); return [state, setState]; };