import { useCallback, useState } from 'react'; import _ from 'lodash'; export function useSet(initialValues: Array = []) { const [uniqueValues, setUniqueValues] = useState>(initialValues); const addTo = useCallback( (valueToAdd: T) => { if (uniqueValues.includes(valueToAdd)) { return; } setUniqueValues([...uniqueValues, valueToAdd]); }, [uniqueValues, setUniqueValues] ); const removeFrom = useCallback( (valueToRemove: T) => { if (!uniqueValues.includes(valueToRemove)) { return; } setUniqueValues(uniqueValues.filter(v => !_.isEqual(v, valueToRemove))); }, [uniqueValues, setUniqueValues] ); return { uniqueValues, addTo, removeFrom }; }