Skip to content

Instantly share code, notes, and snippets.

@TianyiLi
Last active May 18, 2020 14:22
Show Gist options
  • Save TianyiLi/ec2049ed6d202958d87598d2758a136f to your computer and use it in GitHub Desktop.
Save TianyiLi/ec2049ed6d202958d87598d2758a136f to your computer and use it in GitHub Desktop.
react-map-and-set-hooks-sample
import { useState, useMemo, useCallback } from 'react';
interface StableActions<K> {
add: (key: K) => void;
remove: (key: K) => void;
reset: () => void;
}
interface Actions<K> extends StableActions<K> {
has: (key: K) => boolean;
}
function useSet<K>(initialValue?: Iterable<K>): [Set<K>, Actions<K>] {
const initialSet = useMemo<Set<K>>(
() => (initialValue === undefined ? new Set() : new Set(initialValue)) as Set<K>,
[initialValue]
);
const [set, setSet] = useState(initialSet);
const stableActions = useMemo<StableActions<K>>(
() => ({
add: key => setSet(prevSet => new Set([...Array.from(prevSet), key])),
remove: key => setSet(prevSet => new Set(Array.from(prevSet).filter(i => i !== key))),
reset: () => setSet(initialSet),
}),
[setSet]
);
const utils = {
has: useCallback(key => set.has(key), [set]),
...stableActions,
} as Actions<K>;
return [set, utils];
};
export default useSet;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment