Skip to content

Commit 8e71033

Browse files
🛒Added the useQueue Hook (#37)
* 🛒 add useQueue Hook * 🛒add useQueue to readme and docs * Update hooks/useQueue.js Co-authored-by: Aromal Anil <aromalanilkannan@gmail.com> * Update hooks/useQueue.js Co-authored-by: Aromal Anil <aromalanilkannan@gmail.com> * 🔧 Update version to 1.7.0 Co-authored-by: Aromal Anil <aromalanilkannan@gmail.com>
1 parent f92d723 commit 8e71033

5 files changed

Lines changed: 80 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ yarn add use-custom-hooks
4545
## 📘 Available Hooks
4646

4747
- [useStack](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usestack)
48+
- [useQueue](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usequeue)
4849
- [useToggle](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usetoggle)
4950
- [usePrevious](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-useprevious)
5051
- [useDebounce](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usedebounce)

docs/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [useLocalStorage](#-uselocalstorage)
66
- [useStack](#-usestack)
7+
- [useQueue](#-usequeue)
78
- [useDebounce](#-usedebounce)
89
- [useDarkMode](#-usedarkmode)
910
- [useToggle](#-usetoggle)
@@ -93,6 +94,42 @@ const LocalValue = () => {
9394

9495
</br>
9596

97+
## 🛒 useQueue
98+
Very similar to useStack, this hook creates and manages queues.
99+
100+
### Usage
101+
102+
```jsx
103+
import React from 'react';
104+
import { useQueue } from 'use-custom-hooks';
105+
106+
function Queue() {
107+
const [queue, enqueue, dequeue] = useQueue([]);
108+
const generateNumber = () => Math.round(Math.random()*100);
109+
return (
110+
<div>
111+
<h1>{queue.map(item => ` ${item} `)}</h1>
112+
<button onClick={() => enqueue(generateNumber())}>Enqueue</button>
113+
<button onClick={dequeue}>Dequeue</button>
114+
</div>
115+
);
116+
}
117+
```
118+
119+
### Parameters
120+
121+
1. `initialValue` (_Array_) : Initial value of the queue.
122+
123+
### Return value
124+
125+
`[queue,enqueue,dequeue]`
126+
127+
1. `queue` (_Array_) : The created queue.
128+
2. `enqueue` (_function_) : Function to add an element to the rear of the queue.
129+
3. `dequeue` (_function_) : Function to remove last element from the queue.
130+
131+
</br>
132+
96133
## 🏀 useDebounce
97134

98135
Convert a normal function to a debounced function.

hooks/useQueue.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { useState } from 'react';
2+
3+
/**
4+
* Custom hook for creating and managing Queues.
5+
*
6+
* @param {Array} initialValue Initial value of the queue.
7+
* @return {Array} Array containing the queue, enqueue and dequeue functions respectively.
8+
*/
9+
const useQueue = (initialValue) => {
10+
const [queue, setQueue] = useState(initialValue);
11+
12+
/**
13+
* Function to add a value to the rear of the queue.
14+
*
15+
* @param {any} value Value to be added to the queue.
16+
* @returns {undefined} This function returns nothing.
17+
*/
18+
const enqueue = (value) => {
19+
setQueue([...queue, value]);
20+
};
21+
22+
/**
23+
* Function to remove the value in the front of the queue.
24+
*
25+
* @returns {any} The value removed from the queue.
26+
*/
27+
const dequeue = () => {
28+
if (queue.length > 0) {
29+
const newQueue = [...queue];
30+
const dequeuedValue = newQueue.shift();
31+
setQueue(newQueue);
32+
return dequeuedValue;
33+
}
34+
return undefined;
35+
};
36+
37+
return [queue, enqueue, dequeue];
38+
};
39+
40+
export default useQueue;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export { default as useGeoLocation } from './hooks/useGeoLocation';
66
export { default as useLocalStorage } from './hooks/useLocalStorage';
77
export { default as useMousePosition } from './hooks/useMousePosition';
88
export { default as usePrevious } from './hooks/usePrevious';
9+
export { default as useQueue } from './hooks/useQueue';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-custom-hooks",
3-
"version": "1.6.2",
3+
"version": "1.7.0",
44
"description": "A collection of Custom Hooks for React.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)