Skip to content

Commit

Permalink
feat(utils): added usePartialState hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bang9 committed Mar 3, 2023
1 parent be44b7b commit f76fe69
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/uikit-utils/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { DependencyList } from 'react';
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useLayoutEffect, useReducer, useRef, useState } from 'react';

type Destructor = () => void;
type AsyncEffectCallback = () => void | Destructor | Promise<void> | Promise<Destructor>;
Expand Down Expand Up @@ -98,3 +98,7 @@ export const useDebounceEffect = (action: () => void, delay: number, deps: Depen
};
}, [delay, ...deps]);
};

export const usePartialState = <S>(initialState: S) => {
return useReducer((prev: S, state: Partial<S>) => ({ ...prev, ...state }), initialState);
};

0 comments on commit f76fe69

Please sign in to comment.