function useAsyncQueuedState<TValue, TSelected>(
fn,
options,
selector?): [TValue[], ReactAsyncQueuer<TValue, TSelected>]
function useAsyncQueuedState<TValue, TSelected>(
fn,
options,
selector?): [TValue[], ReactAsyncQueuer<TValue, TSelected>]
定义于: react-pacer/src/async-queuer/useAsyncQueuedState.ts:151
一个更高级别的 React Hook,它创建一个内置状态管理的 AsyncQueuer 实例。
此 Hook 将 AsyncQueuer 与 React 状态相结合,以自动跟踪队列项。它返回一个元组,包含
队列可以配置为
每当项被
时,状态将自动更新。此 Hook 使用 TanStack Store 通过底层的 async queuer 实例进行响应式状态管理。 selector 参数允许您指定哪些 async queuer 状态更改会触发重新渲染,通过防止不相关的状态更改导致的不必要重新渲染来优化性能。
默认情况下,将不会进行响应式状态订阅,您必须通过提供 selector 函数来选择加入状态跟踪。这可以防止不必要的重新渲染,并让您完全控制组件何时更新。只有当您提供 selector 时,组件才会在我选定的状态值更改时重新渲染。
可用的 async queuer 状态属性
• TValue
• TSelected extends Pick<AsyncQueuerState<TValue>, "items"> = Pick<AsyncQueuerState<TValue>, "items">
(value) => Promise<any>
AsyncQueuerOptions<TValue> = {}
(state) => TSelected
[TValue[], ReactAsyncQueuer<TValue, TSelected>]
// Default behavior - no reactive state subscriptions
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{
concurrency: 2,
maxSize: 100,
started: true
}
);
// Opt-in to 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
})
);
// Opt-in to 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
})
);
// Opt-in to 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
})
);
// Opt-in to 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 (will be empty object {} unless selector provided)
const { size, isRunning, activeItems } = asyncQueuer.state;
// Default behavior - no reactive state subscriptions
const [queueItems, asyncQueuer] = useAsyncQueuedState(
async (item) => {
const result = await processItem(item);
return result;
},
{
concurrency: 2,
maxSize: 100,
started: true
}
);
// Opt-in to 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
})
);
// Opt-in to 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
})
);
// Opt-in to 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
})
);
// Opt-in to 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 (will be empty object {} unless selector provided)
const { size, isRunning, activeItems } = asyncQueuer.state;
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。