Skip to content

Commit

Permalink
feat: 🎸 add clear() to useList, use fn for state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 28, 2019
1 parent 676d0de commit b20cf7c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {useState} from 'react';

export interface Actions<T> {
set: (list: T[]) => void;
clear: () => void;
updateAt: (index: number, item: T) => void;
remove: (index: number) => void;
push: (item: T) => void;
Expand All @@ -14,18 +15,19 @@ const useList = <T>(initialList: T[] = []): [T[], Actions<T>] => {

return [list, {
set,
updateAt: (index, entry) => set([
clear: () => set([]),
updateAt: (index, entry) => set(list => [
...list.slice(0, index),
entry,
...list.slice(index + 1)
]),
remove: (index) => set([
remove: (index) => set(list => [
...list.slice(0, index),
...list.slice(index + 1)
]),
push: (entry) => set([...list, entry]),
filter: (fn) => set(list.filter(fn)),
sort: (fn?) => set([...list].sort(fn)),
push: (entry) => set(list => [...list, entry]),
filter: (fn) => set(list => list.filter(fn)),
sort: (fn?) => set(list => [...list].sort(fn)),
}];
};

Expand Down

0 comments on commit b20cf7c

Please sign in to comment.