@utilityjs/use-immutable-array
v1.0.1
Published
A React hook that creates an array with immutable operations.
Downloads
5
Maintainers
Readme
A React hook that creates an array with immutable operations.
npm i @utilityjs/use-immutable-array | yarn add @utilityjs/use-immutable-array
Usage
const array = useImmutableArray([1, 2, 3, 4, 5]);
array.push(6) // [1, 2, 3, 4, 5, 6]
array.pop() // [1, 2, 3, 4, 5]
array.shift() // [2, 3, 4, 5]
array.shift(10) // [10, 2, 3, 4, 5]
array.reverse() // [5, 4, 3, 2, 10]
array.removeByIndex(1) // [5, 3, 2, 10]
array.removeByValue(10) // [5, 3, 2]
array.filter(item => item % 2 !== 0) // [5, 3]
array.insertItem(1, 7) // [5, 7, 3]
array.moveItem(0, 1) // [7, 5, 3]
array.values // [7, 5, 3]
array.setValues([1, 2, 3, 4]) // [1, 2, 3, 4]
API
useImmutableArray(array)
interface Return<T> {
pop: () => void;
push: (value: T) => void;
shift: () => void;
unshift: (value: T) => void;
reverse: () => void;
removeByIndex: (index: number) => void;
removeByValue: (value: T) => void;
filter: (
predicate: (value: T, index: number, array: T[]) => value is T,
thisArg?: any
) => void;
insertItem: (index: number, value: T) => void;
moveItem: (fromIndex: number, toIndex: number) => void;
values: T[];
setValues: SetArrayValues<T>;
}
declare const useImmutableArray: <T>(array: T[]) => Return<T>;
array
The initial value of the immutable-array.