mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
13 lines
400 B
TypeScript
13 lines
400 B
TypeScript
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 => {
|
|
set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch));
|
|
};
|
|
|
|
return [state, setState];
|
|
};
|
|
|
|
export default useSetState;
|