use-array-state
Version:
Provides a reducer to simplify handling of mutations to array state
64 lines (63 loc) • 2.8 kB
TypeScript
import * as actions from './actions';
interface UseArrayStateOperations<T extends any> {
/**
* Appends `value` to end
*/
push(value: T): void;
/**
* Appends `value` to beginning
*/
unshift(value: T): void;
/**
* Invokes `Array.prototype.splice` without mutating original value
*/
splice(start: number, deleteCount?: number, ...values: T[]): void;
/**
* Invokes `Array.prototype.pop` without mutating original value
*/
pop(): void;
/**
* Invokes `Array.prototype.shift` without mutating original value
*/
shift(): void;
/**
* Updates value at index
*/
update(index: number, value: T): void;
/**
* Replace the entire value of state, passed through second param of `useState`
*/
set(values: T[]): void;
/**
* Inserts `value` at `index`
*/
insert(index: number, value: T): void;
/**
* Moves value at index `from` to position `to`
* If the `to` position is longer then the array, the array will grow
* with undefined values to accomodate
*/
move(from: number, to: number): void;
/**
* Removes element at `index`
*/
remove(index: number): void;
/**
* Swap elements to new positions in the array
* If either index is longer then the array, the array will with
* undefined values to accomodate
*/
swap(indexA: number, indexB: number): void;
}
export declare const reducer: (state: any[], action: import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.PUSH, any> | import("typesafe-actions/dist/type-helpers").EmptyAction<actions.ACTION_TYPES.POP> | import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.UNSHIFT, any> | import("typesafe-actions/dist/type-helpers").EmptyAction<actions.ACTION_TYPES.SHIFT> | import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.SPLICE, [number, number, ...any[]]> | import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.UPDATE, {
index: number;
value: any;
}> | import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.SET, any[]> | import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.INSERT, {
index: number;
value: any;
}> | import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.MOVE, {
from: number;
to: number;
}> | import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.REMOVE, number> | import("typesafe-actions/dist/type-helpers").PayloadAction<actions.ACTION_TYPES.SWAP, [number, number]>) => any[];
export default function useArrayState<T extends any>(initialState?: T[]): [T[], UseArrayStateOperations<T>];
export {};