The useQueue hook is a custom React hook designed to manage a queue with a fixed limit for the visible state. It provides functionalities to add new items, update the queue, and clean up any pending items in the queue. This is particularly useful for managing paginated data or tasks where you need to handle overflow dynamically.
import { useQueue } from 'hook-master-react';const { state, queue, add, update, cleanQueue } = useQueue({ initialValues: [1], limit: 2,});// state -> [1], queue -> []// When state.length is less that limit, new items are added to stateadd(2);// state -> [1,2], queue -> []// When state.length is equal to limit, new items are added to queueadd(3, 4, 5, 6);// state -> [1,2], queue -> [3,4,5,6]// Use update function to modify itemsupdate((values) => values.map((item) => item * 3));// state -> [3,6], queue -> [9,12,15,18]// If you add or remove items in update function,// they will be divided between queue and state according to limit// order is always preservedupdate((values) => values.filter((item) => item % 2));// state -> [3,9], queue -> [15]// Remove all items from queuecleanQueue();// state -> [3,9], queue -> []// Remove all items from queue and stateupdate(() => []);// state -> [], queue -> []