const useSetState = <T extends dataStructure>(
initialState: T = {} as T
): [T, (patch: Partial<T> | ((prevState: T) => Partial<T>)) => void] => {
const [state, setState] = useState<T>(initialState);
const setMergeState = useCallback(
(patch) => {
setState((prevState) => ({
...prevState,
...(patch instanceof Function ? patch(prevState) : patch),
}));
},
[setState]
);
return [state, setMergeState];
};
However, an issue arose: Avoid using dataStructure
as a type. The dataStructure
type can be challenging to work with (see this issue).