useList

Manage and manipulate lists with useList.

Install:

npm i @uidotdev/usehooks

Description:

The useList hook is useful for managing and manipulating lists in a React component. It encapsulates the list state and provides a set of convenient methods to modify the list. By using this hook, you can easily initialize a list with a default value and access various methods to add, remove, update, or clear elements within the list.

Parameters

NameTypeDescription
defaultListarrayThe initial list of elements. Default is an empty array.

Return Value

The useList hook returns an array with two elements:

NameParametersDescription
[0]The current list of elements.
[1].setl: arrayReplaces the entire list with a new array l.
[1].pushelement: anyAppends the element to the end of the list.
[1].removeAtindex: numberRemoves the element at the specified index from the list.
[1].insertAtindex: number, element: anyInserts the element at the specified index in the list.
[1].updateAtindex: number, element: anyReplaces the element at the specified index with the element.
[1].clearRemoves all elements from the list.

Demo:

Example:

More Hooks:

Sort: