function useAsyncQueuedState<TValue, TSelected>(
fn,
options,
selector?): [TValue[], ReactAsyncQueuer<TValue, TSelected>]
function useAsyncQueuedState<TValue, TSelected>(
fn,
options,
selector?): [TValue[], ReactAsyncQueuer<TValue, TSelected>]
Defined in: react-pacer/src/async-queuer/useAsyncQueuedState.ts:150
A higher-level React hook that creates an AsyncQueuer instance with built-in state management.
This hook combines an AsyncQueuer with React state to automatically track the queue items. It returns a tuple containing:
The queue can be configured with:
The state will automatically update whenever items are:
The hook uses TanStack Store for reactive state management via the underlying async queuer instance. The selector parameter allows you to specify which async queuer state changes will trigger a re-render, optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
By default, all async queuer 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 async queuer state properties:
• TValue
• TSelected extends Pick<AsyncQueuerState<TValue>, "items"> = AsyncQueuerState<TValue>
(value) => Promise<any>
AsyncQueuerOptions<TValue> = {}
(state) => TSelected
[TValue[], ReactAsyncQueuer<TValue, TSelected>]
// Create a queue with state management (re-renders on any async queuer state change)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{
concurrency: 2,
maxSize: 100,
started: true
}
);
// Only re-render when queue contents change (optimized for displaying queue items)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: true },
(state) => ({
items: state.items,
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Only re-render when processing state changes (optimized for loading indicators)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: true },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
activeItems: state.activeItems,
pendingTick: state.pendingTick
})
);
// Only re-render when execution metrics change (optimized for stats display)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: true },
(state) => ({
successCount: state.successCount,
errorCount: state.errorCount,
settledCount: state.settledCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Only re-render when results are available (optimized for data display)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: true },
(state) => ({
lastResult: state.lastResult,
successCount: state.successCount
})
);
// Add items to queue - state updates automatically
asyncQueuer.addItem(async () => {
const result = await fetchData();
return result;
});
// Start processing
asyncQueuer.start();
// Stop processing
asyncQueuer.stop();
// queueItems reflects current queue state
const pendingCount = asyncQueuer.peekPendingItems().length;
// Access the selected async queuer state
const { size, isRunning, activeItems } = asyncQueuer.state;
// Create a queue with state management (re-renders on any async queuer state change)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{
concurrency: 2,
maxSize: 100,
started: true
}
);
// Only re-render when queue contents change (optimized for displaying queue items)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: true },
(state) => ({
items: state.items,
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Only re-render when processing state changes (optimized for loading indicators)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: true },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
activeItems: state.activeItems,
pendingTick: state.pendingTick
})
);
// Only re-render when execution metrics change (optimized for stats display)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: true },
(state) => ({
successCount: state.successCount,
errorCount: state.errorCount,
settledCount: state.settledCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Only re-render when results are available (optimized for data display)
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: true },
(state) => ({
lastResult: state.lastResult,
successCount: state.successCount
})
);
// Add items to queue - state updates automatically
asyncQueuer.addItem(async () => {
const result = await fetchData();
return result;
});
// Start processing
asyncQueuer.start();
// Stop processing
asyncQueuer.stop();
// queueItems reflects current queue state
const pendingCount = asyncQueuer.peekPendingItems().length;
// Access the selected async queuer state
const { size, isRunning, activeItems } = asyncQueuer.state;
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.