Files
HKSingleParty/03_source/mobile/src/hooks/use-set-state.ts

33 lines
1.1 KiB
TypeScript

/**
* Custom hook to manage state with utility functions to set state, set a specific field, and reset state.
*
* @param {T} initialState - The initial state value.
*
* @returns {UseSetStateReturn<T>} - An object containing:
* - `state`: The current state.
* - `resetState`: A function to reset the state to the initial value.
* - `setState`: A function to update the state.
* - `setField`: A function to update a specific field in the state.
*
* @example
* const { state, setState, setField, resetState } = useSetState({ name: '', age: 0 });
*
* return (
* <div>
* <p>Name: {state.name}</p>
* <p>Age: {state.age}</p>
* <button onClick={() => setField('name', 'John')}>Set Name</button>
* <button onClick={resetState}>Reset</button>
* </div>
* );
*/
type UseSetStateReturn<T> = {
state: T;
resetState: (defaultState?: T) => void;
setState: (updateState: T | Partial<T>) => void;
setField: (name: keyof T, updateValue: T[keyof T]) => void;
};
declare function useSetState<T>(initialState?: T): UseSetStateReturn<T>;
export { type UseSetStateReturn, useSetState };