react-usehandlestate
Version:
A modified useState hook which exposes the commonly implmented handleChange function which can be used to modify state. handleChange supports modifying properties of objects stored in state including nested objects and their properties.
11 lines (9 loc) • 579 B
text/typescript
type Path<T> = T extends object ? {
[K in keyof T & string]: T[K] extends object ? K | `${K}.${Path<T[K]>}` : K;
}[keyof T & string] : never;
type PathValue<T, P extends string> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? PathValue<T[K], Rest> : never : P extends keyof T ? T[P] : never;
declare function useHandleState<S>(initialState: S | (() => S)): readonly [S, {
(newValue: S | ((prev: S) => S)): void;
<P extends Path<S>>(path: P, newValue: PathValue<S, P> | ((prev: PathValue<S, P>) => PathValue<S, P>)): void;
}];
export { useHandleState };