-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🛒 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>
- Loading branch information
1 parent
f92d723
commit 8e71033
Showing
5 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useState } from 'react'; | ||
|
||
/** | ||
* Custom hook for creating and managing Queues. | ||
* | ||
* @param {Array} initialValue Initial value of the queue. | ||
* @return {Array} Array containing the queue, enqueue and dequeue functions respectively. | ||
*/ | ||
const useQueue = (initialValue) => { | ||
const [queue, setQueue] = useState(initialValue); | ||
|
||
/** | ||
* Function to add a value to the rear of the queue. | ||
* | ||
* @param {any} value Value to be added to the queue. | ||
* @returns {undefined} This function returns nothing. | ||
*/ | ||
const enqueue = (value) => { | ||
setQueue([...queue, value]); | ||
}; | ||
|
||
/** | ||
* Function to remove the value in the front of the queue. | ||
* | ||
* @returns {any} The value removed from the queue. | ||
*/ | ||
const dequeue = () => { | ||
if (queue.length > 0) { | ||
const newQueue = [...queue]; | ||
const dequeuedValue = newQueue.shift(); | ||
setQueue(newQueue); | ||
return dequeuedValue; | ||
} | ||
return undefined; | ||
}; | ||
|
||
return [queue, enqueue, dequeue]; | ||
}; | ||
|
||
export default useQueue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters