From 524abe5702198926a2021cda7941b0f4141ed6f3 Mon Sep 17 00:00:00 2001 From: streamich Date: Sun, 17 Feb 2019 09:19:40 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20useSetState=20after?= =?UTF-8?q?=20React=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As reported in #107 Issues: #107 --- src/__stories__/useSetState.story.tsx | 4 ++-- src/useSetState.ts | 12 +++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/__stories__/useSetState.story.tsx b/src/__stories__/useSetState.story.tsx index b8eb61fb49..b4cb7ce40e 100644 --- a/src/__stories__/useSetState.story.tsx +++ b/src/__stories__/useSetState.story.tsx @@ -11,14 +11,14 @@ const Demo = () => {
{JSON.stringify(state, null, 2)}
- ); diff --git a/src/useSetState.ts b/src/useSetState.ts index 77d7274b74..2ec750a83f 100644 --- a/src/useSetState.ts +++ b/src/useSetState.ts @@ -2,15 +2,9 @@ import {useState} from 'react'; const useSetState = (initialState: T = {} as T): [T, (patch: Partial | Function) => void]=> { const [state, set] = useState(initialState); - const setState = (patch) => { - if (patch instanceof Function) { - set((prevState) => { - return Object.assign(state, patch(prevState)) - }) - } else { - Object.assign(state, patch); - set(state); - } + const setState = patch => { + if (patch instanceof Function) set(prevState => Object.assign({}, prevState, patch(prevState))); + else set(Object.assign({}, state, patch)); }; return [state, setState];