fix(striker-ui): add default protect() to useProtectedState

main
Tsu-ba-me 2 years ago
parent d45c0374fc
commit e56fcfd880
  1. 15
      striker-ui/hooks/useProtectedState.ts

@ -1,4 +1,6 @@
import { Dispatch, SetStateAction, useState } from 'react';
import { Dispatch, SetStateAction, useMemo, useState } from 'react';
import useProtect from './useProtect';
type SetStateFunction<S> = Dispatch<SetStateAction<S>>;
@ -8,17 +10,24 @@ type SetStateReturnType<S> = ReturnType<SetStateFunction<S>>;
const useProtectedState = <S>(
initialState: S | (() => S),
protect: (
protect?: (
fn: SetStateFunction<S>,
...args: SetStateParameters<S>
) => SetStateReturnType<S>,
): [S, SetStateFunction<S>] => {
const { protect: defaultProtect } = useProtect();
const [state, setState] = useState<S>(initialState);
const pfn = useMemo(
() => protect ?? defaultProtect,
[defaultProtect, protect],
);
return [
state,
(...args: SetStateParameters<S>): SetStateReturnType<S> =>
protect(setState, ...args),
pfn(setState, ...args),
];
};

Loading…
Cancel
Save