Skip to content

Commit b20cf7c

Browse files
committed
feat: 🎸 add clear() to useList, use fn for state updates
1 parent 676d0de commit b20cf7c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/useList.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {useState} from 'react';
22

33
export interface Actions<T> {
44
set: (list: T[]) => void;
5+
clear: () => void;
56
updateAt: (index: number, item: T) => void;
67
remove: (index: number) => void;
78
push: (item: T) => void;
@@ -14,18 +15,19 @@ const useList = <T>(initialList: T[] = []): [T[], Actions<T>] => {
1415

1516
return [list, {
1617
set,
17-
updateAt: (index, entry) => set([
18+
clear: () => set([]),
19+
updateAt: (index, entry) => set(list => [
1820
...list.slice(0, index),
1921
entry,
2022
...list.slice(index + 1)
2123
]),
22-
remove: (index) => set([
24+
remove: (index) => set(list => [
2325
...list.slice(0, index),
2426
...list.slice(index + 1)
2527
]),
26-
push: (entry) => set([...list, entry]),
27-
filter: (fn) => set(list.filter(fn)),
28-
sort: (fn?) => set([...list].sort(fn)),
28+
push: (entry) => set(list => [...list, entry]),
29+
filter: (fn) => set(list => list.filter(fn)),
30+
sort: (fn?) => set(list => [...list].sort(fn)),
2931
}];
3032
};
3133

0 commit comments

Comments
 (0)