Skip to content

Commit 97279f6

Browse files
committed
[FIX] useStateGetReset without initial state
1 parent a7ac352 commit 97279f6

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

apps/react-tools-demo/src/pages/hooks/state/useStateGetReset/UseStateGetReset.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The two functions onChange and onChange Getter update their respective _state_ e
1515
*/
1616
const UseStateGetReset = () => {
1717
const [stateG, setStateG, getState, resetState] = useStateGetReset({ id: "", name: "", eta: "" });
18-
const [state, setState] = useState({ id: "", name: "", eta:"" });
18+
const [state, setState] = useState({ id: "", name: "", eta: "" });
1919

2020
const onChangeGetter = useCallback((e: BaseSyntheticEvent) => {
2121
const state = getState();

packages/react-tools-lib/src/hooks/state/useStateGetReset.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { useMemoizedFn } from "../performance";
66
* @param {T | () => T} initialState - value or a function.
77
* @returns {[T, Dispatch<SetStateAction<T>>, () => T, ()=>void]} array
88
*/
9-
export const useStateGetReset = <T>(initialState: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T, () => void] => {
10-
const [state, setState] = useState<T>(initialState);
11-
const getter = useMemoizedFn<()=>T>(() => state);
9+
function useStateGetReset<T = undefined>(initialState?: undefined): [T | undefined, Dispatch<SetStateAction<T | undefined>>, () => T | undefined, () => void];
10+
function useStateGetReset<T>(initialState?: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T, () => void];
11+
function useStateGetReset<T>(initialState?: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T, () => void] | [T | undefined, Dispatch<SetStateAction<T | undefined>>, () => T | undefined, () => void] {
12+
const [state, setState] = useState<T | undefined>(initialState);
13+
const getter = useMemoizedFn<()=>T | undefined>(() => state);
1214

1315
const resetter = useCallback(() => setState(initialState), [initialState]);
1416

@@ -18,4 +20,6 @@ export const useStateGetReset = <T>(initialState: T | (() => T)): [T, Dispatch<S
1820
getter,
1921
resetter
2022
];
21-
}
23+
}
24+
25+
export { useStateGetReset };

0 commit comments

Comments
 (0)