Skip to content

Commit

Permalink
feat: Add updateAt method for useList
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich authored Feb 7, 2019
2 parents 682a271 + 5c72b7c commit ad05609
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 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;
updateAt: (index: number, item: T) => void;
push: (item: T) => void;
filter: (fn: (value: T) => boolean) => void;
sort: (fn?: (a: T, b: T) => number) => void;
Expand All @@ -12,6 +13,11 @@ const useList = <T>(initialList: T[] = []): [T[], Actions<T>] => {

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

0 comments on commit ad05609

Please sign in to comment.