Skip to content

Instantly share code, notes, and snippets.

@mrkrstphr
Created June 11, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkrstphr/2215c37e092fa3d317b5140bdaa9598b to your computer and use it in GitHub Desktop.
Save mrkrstphr/2215c37e092fa3d317b5140bdaa9598b to your computer and use it in GitHub Desktop.

I made this once, but never used it. I'm dropping it here incase I need it someday.

Usage

const {
  collection,
  addItem,
  removeItem,
  setItems,
} = useManageableCollection([]);
function useManageableCollection(initialValues = []) {
const [collection, setCollection] = useState(initialValues);
const addItem = (item) => setCollection([...collection, item]);
const removeItem = (index) =>
setCollection(collection.filter((item, testIndex) => index !== testIndex));
const clearItems = () => setCollection([]);
return {
collection,
addItem,
removeItem,
setItems: setCollection,
clearItems,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment