function useQueuedValue<TValue, TSelected>(
initialValue,
options,
selector?): [TValue, ReactQueuer<TValue, TSelected>]
function useQueuedValue<TValue, TSelected>(
initialValue,
options,
selector?): [TValue, ReactQueuer<TValue, TSelected>]
定义于: react-pacer/src/queuer/useQueuedValue.ts:103
一个 React Hook,用于创建一个队列值,该值按顺序处理状态更改,并可选择延迟。此 Hook 内部使用 useQueuer 来管理状态更改队列并按顺序应用它们。
队列值将按接收顺序处理更改,并且在处理每次更改之间可选择延迟。这对于处理需要按特定顺序处理的状态更新非常有用,例如动画或连续的 UI 更新。
该 hook 返回一个元组,包含:
该 Hook 使用 TanStack Store 通过底层的队列实例进行响应式状态管理。`selector` 参数允许您指定哪些队列状态更改将触发重新渲染,通过防止不相关的状态更改发生不必要的重新渲染来优化性能。
默认情况下,不会进行响应式状态订阅,您必须通过提供 `selector` 函数来选择加入状态跟踪。这可以防止不必要的重新渲染,并让您完全控制组件何时更新。只有当您提供 `selector` 时,组件才会在选定的状态值更改时重新渲染。
可用的队列状态属性
• TValue
• TSelected 扩展 Pick<QueuerState<TValue>, "items"> = Pick<QueuerState<TValue>, "items">
TValue
QueuerOptions<TValue> = {}
(state) => TSelected
[TValue, ReactQueuer<TValue, TSelected>]
// Default behavior - no reactive state subscriptions
const [value, queuer] = useQueuedValue(initialValue, {
wait: 500, // Wait 500ms between processing each change
started: true // Start processing immediately
});
// Opt-in to re-render when queue processing state changes (optimized for loading indicators)
const [value, queuer] = useQueuedValue(
initialValue,
{ wait: 500, started: true },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
pendingTick: state.pendingTick
})
);
// Opt-in to re-render when queue contents change (optimized for displaying queue status)
const [value, queuer] = useQueuedValue(
initialValue,
{ wait: 500, started: true },
(state) => ({
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Opt-in to re-render when execution metrics change (optimized for stats display)
const [value, queuer] = useQueuedValue(
initialValue,
{ wait: 500, started: true },
(state) => ({
executionCount: state.executionCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Add changes to the queue
const handleChange = (newValue) => {
queuer.addItem(newValue);
};
// Control the queue
const pauseProcessing = () => {
queuer.stop();
};
const resumeProcessing = () => {
queuer.start();
};
// Access the selected queuer state (will be empty object {} unless selector provided)
const { size, isRunning, executionCount } = queuer.state;
// Default behavior - no reactive state subscriptions
const [value, queuer] = useQueuedValue(initialValue, {
wait: 500, // Wait 500ms between processing each change
started: true // Start processing immediately
});
// Opt-in to re-render when queue processing state changes (optimized for loading indicators)
const [value, queuer] = useQueuedValue(
initialValue,
{ wait: 500, started: true },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
pendingTick: state.pendingTick
})
);
// Opt-in to re-render when queue contents change (optimized for displaying queue status)
const [value, queuer] = useQueuedValue(
initialValue,
{ wait: 500, started: true },
(state) => ({
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Opt-in to re-render when execution metrics change (optimized for stats display)
const [value, queuer] = useQueuedValue(
initialValue,
{ wait: 500, started: true },
(state) => ({
executionCount: state.executionCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Add changes to the queue
const handleChange = (newValue) => {
queuer.addItem(newValue);
};
// Control the queue
const pauseProcessing = () => {
queuer.stop();
};
const resumeProcessing = () => {
queuer.start();
};
// Access the selected queuer state (will be empty object {} unless selector provided)
const { size, isRunning, executionCount } = queuer.state;
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。