File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 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
98135Convert a normal function to a debounced function.
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change @@ -6,3 +6,4 @@ export { default as useGeoLocation } from './hooks/useGeoLocation';
66export { default as useLocalStorage } from './hooks/useLocalStorage' ;
77export { default as useMousePosition } from './hooks/useMousePosition' ;
88export { default as usePrevious } from './hooks/usePrevious' ;
9+ export { default as useQueue } from './hooks/useQueue' ;
Original file line number Diff line number Diff line change 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" : {
You can’t perform that action at this time.
0 commit comments