Skip to content

Instantly share code, notes, and snippets.

@PascalSenn
Created October 24, 2019 08:34
Show Gist options
  • Save PascalSenn/2339dd976283a0beda59ab040f5b7e0d to your computer and use it in GitHub Desktop.
Save PascalSenn/2339dd976283a0beda59ab040f5b7e0d to your computer and use it in GitHub Desktop.
ObjectStatehook
function useObjectState<T, K extends keyof T>(
key: K,
objState: [T, Dispatch<SetStateAction<T>>]
): [T[K], (val: T[K]) => void] {
const [obj, setObj] = objState;
const setKey = (val: T[K]) => setObj({ ...obj, [key]: val });
return [obj[key], setKey];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment