- Hooks
- Browser hooks
- useList
useList
Manage and manipulate lists with useList.
use-list.tsx
Installation
Copy and paste the following code into your project.
use-list.ts
Update the import paths to match your project setup.
Parameters
Name | Type | Description |
---|---|---|
defaultList | array | The initial list of elements. Default is an empty array. |
Return values
The useList hook returns an array with two elements:
Name | Type | Params | Description |
---|---|---|---|
[0] | array | The current list of elements. | |
[1].set | function | l: array` | Replaces the entire list with a new array l. |
[1].push | function | element: any | Appends the element to the end of the list.. |
[1].removeAt | function | index: number | Removes the element at the specified index from the list. |
[1].insertAt | function | index: number, element: any | Inserts the element at the specified index in the list. |
[1].updateAt | function | index: number, element: any | Replaces the element at the specified index with the element.. |
[1].clear | function | Removes all elements from the list. |
Usage
const [currentArray, { set, push, removeAt, insertAt, updateAt, clear }] = useList([1, 2, 3, 4, 5]);