useQueue

Add, remove, and clear element from a queue data structure with useQueue.

Install:

npm i @uidotdev/usehooks

Description:

The useQueue hook is a useful tool for managing a queue of elements within a functional component. It allows you to easily add, remove, and clear elements from the queue while maintaining the necessary state updates. The hook returns an object that includes the functions for manipulating the queue (add, remove, and clear), as well as additional properties such as first, last, size, and queue.

Parameters

NameTypeDescription
initialValuearray(Optional) The initial value for the queue. Default is an empty array.

Return Value

The useQueue hook returns an object with the following properties and methods:

NameTypeDescription
addfunctionAdds an element to the end of the queue.
removefunctionRemoves and returns the first element from the queue.
clearfunctionClears the queue, removing all elements.
firstanyThe first element in the queue.
lastanyThe last element in the queue.
sizenumberThe number of elements in the queue.
queuearrayThe current array representing the queue.

Demo:

Example:

More Hooks:

Sort: