Skip to content

Latest commit

Β 

History

History
85 lines (73 loc) Β· 3.36 KB

useList.md

File metadata and controls

85 lines (73 loc) Β· 3.36 KB

useList

Tracks an array and provides methods to modify it.
To cause component re-render you have to use these methods instead of direct interaction with array - it won't cause re-render.

We can ensure that actions object and actions itself will not mutate or change between renders, so there is no need to add it to useEffect dependencies and safe to pass them down to children.

Usage

import {useList} from 'vue-next-use';

const Demo = {
    setup() {

        const [list, {
            set,
            push,
            updateAt,
            insertAt,
            update,
            updateFirst,
            upsert,
            sort,
            filter,
            removeAt,
            clear,
            reset
        }] = useList([1, 2, 3, 4, 5]);

        return () => (
            <div>
                <button onClick={() => set([1, 2, 3])}>Set to [1, 2, 3]</button>
                <button onClick={() => push(Date.now())}>Push timestamp</button>
                <button onClick={() => updateAt(1, Date.now())}>Update value at index 1</button>
                <button onClick={() => removeAt(1)}>Remove element at index 1</button>
                <button onClick={() => filter(item => item % 2 === 0)}>Filter even values</button>
                <button onClick={() => sort((a, b) => a - b)}>Sort ascending</button>
                <button onClick={() => sort((a, b) => b - a)}>Sort descending</button>
                <button onClick={clear}>Clear</button>
                <button onClick={reset}>Reset</button>
                <pre>{JSON.stringify(list.value, null, 2)}</pre>
            </div>
        );
    }
};

Reference

import {useList} from "vue-next-use";

const [list, { 
    set, 
    push, 
    updateAt, 
    insertAt, 
    update, 
    updateFirst,
    upsert, 
    sort, 
    filter, 
    removeAt, 
    clear, 
    reset 
}] = useList(array: any[] | ()=> any[]);
  • list: T{} β€” current list;
  • set: (list: T[]) => void; β€” Set new list instead old one;
  • push: (...items: T[]) => void; β€” Add item(s) at the end of list;
  • updateAt: (index: number, item: T) => void; β€” Replace item at given position. If item at given position not exists it will be set;
  • insertAt: (index: number, item: T) => void; β€” Insert item at given position, all items to the right will be shifted;
  • update: (predicate: (a: T, b: T) => boolean, newItem: T) => void; β€” Replace all items that matches predicate with given one;
  • updateFirst: (predicate: (a: T, b: T) => boolean, newItem: T) => void; β€” Replace first item matching predicate with given one;
  • upsert: (predicate: (a: T, b: T) => boolean, newItem: T) => void; β€” Like updateFirst but in case of predicate miss - pushes item to the list;
  • sort: (compareFn?: (a: T, b: T) => number) => void; β€” Sort list with given sorting function;
  • filter: (callbackFn: (value: T, index?: number, array?: T[]) => boolean, thisArg?: any) => void; β€” Same as native Array's method;
  • removeAt: (index: number) => void; β€” Removes item at given position. All items to the right from removed will be shifted;
  • clear: () => void; β€” Make the list empty;
  • reset: () => void; β€” Reset list to initial value;

Related hooks