diff --git a/src/useList.ts b/src/useList.ts index 532bb1de90..56124f0a05 100644 --- a/src/useList.ts +++ b/src/useList.ts @@ -2,6 +2,7 @@ import {useState} from 'react'; export interface Actions { set: (list: T[]) => void; + clear: () => void; updateAt: (index: number, item: T) => void; remove: (index: number) => void; push: (item: T) => void; @@ -14,18 +15,19 @@ const useList = (initialList: T[] = []): [T[], Actions] => { 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)), }]; };