useQueue
Manage a queue of items
The useQueue
hook is helpful when you want to manage a queue of items (based on queue data structure).
It provide with helper methods like add
, clear
, remove
, set
, reset
and additional properties like firstItem
, lastItem
and size
.
useQueue
First Item
Last Item
Single Item
0
Usage
Import the hook from @abhushanaj/react-hooks
and use in required component.
API Reference
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
initialQueue | Array<T> (optional) | The initial queue with items prefilled. | [] |
Return Value
Parameter | Type | Description | Default |
---|---|---|---|
queue | Array<T> | The current queue. | N/A |
queueControls | QueueControls | The control options for the queue to add, remove etc. | N/A |
Types Reference
1. QueueControls
Parameter | Type | Description | Default |
---|---|---|---|
firstItem | T|undefined | The first item inserted into queue. | N/A |
lastItem | T|undefined | The last item inserted into queue. | N/A |
size | number | The size of the queue. | N/A |
clear | ()=>void | Clears the queue to an empty state. | N/A |
add | (newItem: T)=>void | Adds a new item to the queue. | N/A |
remove | ()=>T|undefined | Removes the first inserted (FIFO) and returns the item. | N/A |
set | (newQueue: Array<T>)=>void | Set the current queue state to new queue passed. | N/A |
reset | ()=>void | Resets the current queue state to the initial queue state. | N/A |