function useQueuer<TValue, TSelected>(
fn,
options,
selector?): ReactQueuer<TValue, TSelected>
function useQueuer<TValue, TSelected>(
fn,
options,
selector?): ReactQueuer<TValue, TSelected>
Defined in: react-pacer/src/queuer/useQueuer.ts:124
A React hook that creates and manages a Queuer instance.
This is a lower-level hook that provides direct access to the Queuer's functionality without any built-in state management. This allows you to integrate it with any state management solution you prefer (useState, Redux, Zustand, etc.) by utilizing the onItemsChange callback.
For a hook with built-in state management, see useQueuedState.
The Queuer extends the base Queue to add processing capabilities. Items are processed synchronously in order, with optional delays between processing each item. The queuer includes an internal tick mechanism that can be started and stopped, making it useful as a scheduler. When started, it will process one item per tick, with an optional wait time between ticks.
By default uses FIFO (First In First Out) behavior, but can be configured for LIFO (Last In First Out) by specifying 'front' position when adding items.
The hook uses TanStack Store for reactive state management. The selector parameter allows you to specify which state changes will trigger a re-render, optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
By default, all state changes will trigger a re-render. To optimize performance, you can provide a selector function that returns only the specific state values your component needs. The component will only re-render when the selected values change.
Available state properties:
• TValue
• TSelected = QueuerState<TValue>
(item) => void
QueuerOptions<TValue> = {}
(state) => TSelected
ReactQueuer<TValue, TSelected>
// Default behavior - re-renders on any state change
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 }
);
// Only re-render when queue size changes (optimized for displaying queue length)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Only re-render when processing state changes (optimized for loading indicators)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
pendingTick: state.pendingTick
})
);
// Only re-render when execution metrics change (optimized for stats display)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
executionCount: state.executionCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Example with custom state management and scheduling
const [items, setItems] = useState([]);
const queue = useQueuer(
(item) => console.log('Processing:', item),
{
started: true, // Start processing immediately
wait: 1000, // Process one item every second
onItemsChange: (queue) => setItems(queue.peekAllItems()),
getPriority: (item) => item.priority // Process higher priority items first
}
);
// Add items to process - they'll be handled automatically
queue.addItem('task1');
queue.addItem('task2');
// Control the scheduler
queue.stop(); // Pause processing
queue.start(); // Resume processing
// Access the selected state
const { size, isRunning, executionCount } = queue.state;
// Default behavior - re-renders on any state change
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 }
);
// Only re-render when queue size changes (optimized for displaying queue length)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Only re-render when processing state changes (optimized for loading indicators)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
pendingTick: state.pendingTick
})
);
// Only re-render when execution metrics change (optimized for stats display)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
executionCount: state.executionCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Example with custom state management and scheduling
const [items, setItems] = useState([]);
const queue = useQueuer(
(item) => console.log('Processing:', item),
{
started: true, // Start processing immediately
wait: 1000, // Process one item every second
onItemsChange: (queue) => setItems(queue.peekAllItems()),
getPriority: (item) => item.priority // Process higher priority items first
}
);
// Add items to process - they'll be handled automatically
queue.addItem('task1');
queue.addItem('task2');
// Control the scheduler
queue.stop(); // Pause processing
queue.start(); // Resume processing
// Access the selected state
const { size, isRunning, executionCount } = queue.state;
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.