File: useQueuedState.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
React
Version
Latest
Search...
+ K
Menu
Getting Started
Guides
API Reference
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference
Debouncer Examples
Throttler Examples
Rate Limiter Examples
Queue Examples
Batcher Examples
TanStack Query Examples
Framework
React
Version
Latest
Menu
Getting Started
Guides
API Reference
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference
Debouncer Examples
Throttler Examples
Rate Limiter Examples
Queue Examples
Batcher Examples
TanStack Query Examples
On this page
Copy Markdown
Function: useQueuedState()
==========================
ts
function useQueuedState<TValue, TSelected>(
fn,
options,
selector?): [TValue[], (item, position?, runOnItemsChange?) => boolean, ReactQueuer<TValue, TSelected>];
function useQueuedState<TValue, TSelected>(
fn,
options,
selector?): [TValue[], (item, position?, runOnItemsChange?) => boolean, ReactQueuer<TValue, TSelected>];
Defined in: react-pacer/src/queuer/useQueuedState.ts:119
A React hook that creates a queuer with managed state, combining React's useState with queuing functionality. This hook provides both the current queue state and queue control methods.
The queue state is automatically updated whenever items are added, removed, or reordered in the queue. All queue operations are reflected in the state array returned by the hook.
The queue can be started and stopped to automatically process items at a specified interval, making it useful as a scheduler. When started, it will process one item per tick, with an optional wait time between ticks.
The hook returns a tuple containing:
State Management and Selector
-----------------------------
The hook uses TanStack Store for reactive state management via the underlying queuer instance. The selector parameter allows you to specify which queuer state changes will trigger a re-render, optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
By default, there will be no reactive state subscriptions and you must opt-in to state tracking by providing a selector function. This prevents unnecessary re-renders and gives you full control over when your component updates. Only when you provide a selector will the component re-render when the selected state values change.
Available queuer state properties:
Type Parameters
---------------
### TValue
TValue
TSelected extends Pick<QueuerState<TValue>, "items"> = Pick<QueuerState<TValue>, "items">
(item) => void
QueuerOptions<TValue> = {}
(state) => TSelected
[TValue[], (item, position?, runOnItemsChange?) => boolean, ReactQueuer
<TValue, TSelected>]
tsx
// Default behavior - no reactive state subscriptions
const [items, addItem, queue] = useQueuedState(
(item) => console.log('Processing:', item),
{
initialItems: ['item1', 'item2'],
started: true,
wait: 1000,
getPriority: (item) => item.priority
}
);
// Opt-in to re-render when queue contents change (optimized for displaying queue items)
const [items, addItem, queue] = useQueuedState(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
items: state.items,
size: state.size,
isEmpty: state.isEmpty
})
);
// Opt-in to re-render when processing state changes (optimized for loading indicators)
const [items, addItem, queue] = useQueuedState(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
pendingTick: state.pendingTick
})
);
// Opt-in to re-render when execution metrics change (optimized for stats display)
const [items, addItem, queue] = useQueuedState(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
executionCount: state.executionCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Add items to queue
const handleAdd = (item) => {
addItem(item);
};
// Start automatic processing
const startProcessing = () => {
queue.start();
};
// Stop automatic processing
const stopProcessing = () => {
queue.stop();
};
// Manual processing still available
const handleProcess = () => {
const nextItem = queue.getNextItem();
if (nextItem) {
processItem(nextItem);
}
};
// Access the selected queuer state (will be empty object {} unless selector provided)
const { size, isRunning, executionCount } = queue.state;
// Default behavior - no reactive state subscriptions
const [items, addItem, queue] = useQueuedState(
(item) => console.log('Processing:', item),
{
initialItems: ['item1', 'item2'],
started: true,
wait: 1000,
getPriority: (item) => item.priority
}
);
// Opt-in to re-render when queue contents change (optimized for displaying queue items)
const [items, addItem, queue] = useQueuedState(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
items: state.items,
size: state.size,
isEmpty: state.isEmpty
})
);
// Opt-in to re-render when processing state changes (optimized for loading indicators)
const [items, addItem, queue] = useQueuedState(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
pendingTick: state.pendingTick
})
);
// Opt-in to re-render when execution metrics change (optimized for stats display)
const [items, addItem, queue] = useQueuedState(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
executionCount: state.executionCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Add items to queue
const handleAdd = (item) => {
addItem(item);
};
// Start automatic processing
const startProcessing = () => {
queue.start();
};
// Stop automatic processing
const stopProcessing = () => {
queue.stop();
};
// Manual processing still available
const handleProcess = () => {
const nextItem = queue.getNextItem();
if (nextItem) {
processItem(nextItem);
}
};
// Access the selected queuer state (will be empty object {} unless selector provided)
const { size, isRunning, executionCount } = queue.state;
