Merge pull request #112 from likun7981/master

fix: useSetState bug
This commit is contained in:
Va Da 2019-02-22 09:31:57 +01:00 committed by GitHub
commit dc6df606dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,8 +3,7 @@ import {useState} from 'react';
const useSetState = <T extends object>(initialState: T = {} as T): [T, (patch: Partial<T> | Function) => void]=> {
const [state, set] = useState<T>(initialState);
const setState = patch => {
if (patch instanceof Function) set(prevState => Object.assign({}, prevState, patch(prevState)));
else set(Object.assign({}, state, patch));
set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch));
};
return [state, setState];