function useRateLimitedState<TValue, TSelected>(
value,
options,
selector?): [TValue, Dispatch<SetStateAction<TValue>>, ReactRateLimiter<Dispatch<SetStateAction<TValue>>, TSelected>]
function useRateLimitedState<TValue, TSelected>(
value,
options,
selector?): [TValue, Dispatch<SetStateAction<TValue>>, ReactRateLimiter<Dispatch<SetStateAction<TValue>>, TSelected>]
定义于: react-pacer/src/rate-limiter/useRateLimitedState.ts:107
一个 React hook,用于创建一个速率限制的状态值,对在时间窗口内的状态更新强制执行硬限制。此 hook 结合了 React 的 useState 和速率限制功能,以提供受控的状态更新。
速率限制是一种简单的“硬限制”方法——它允许所有更新直到达到限制,然后阻止后续更新,直到窗口重置。与节流或防抖不同,它不尝试间隔或智能地合并更新。这可能导致快速更新的爆发,随后是无更新的时期。
速率限制器支持两种类型的窗口
为了更平滑的更新模式,请考虑使用
速率限制主要用于需要强制执行严格限制的情况,例如 API 速率限制。
该 hook 返回一个元组,包含:
为了在没有状态管理的情况下更直接地控制速率限制,请考虑使用更底层的 useRateLimiter hook。
该 hook 使用 TanStack Store 通过底层的 rate limiter 实例进行响应式状态管理。 `selector` 参数允许您指定哪些 rate limiter 状态更改将触发重新渲染,通过防止不相关的状态更改导致的不必要重新渲染来优化性能。
默认情况下,将没有响应式状态订阅,您必须通过提供选择器函数来选择加入状态跟踪。这可以防止不必要的重新渲染,并让您完全控制组件何时更新。只有当您提供选择器时,组件才会在选定的状态值更改时重新渲染。
可用的速率限制器状态属性
• TValue
• TSelected = RateLimiterState
TValue
RateLimiterOptions<Dispatch<SetStateAction<TValue>>>
(state) => TSelected
[TValue, Dispatch<SetStateAction<TValue>>, ReactRateLimiter<Dispatch<SetStateAction<TValue>>, TSelected>]
// Default behavior - no reactive state subscriptions
const [value, setValue, rateLimiter] = useRateLimitedState(0, {
limit: 5,
window: 60000,
windowType: 'sliding'
});
// Opt-in to re-render when execution count changes (optimized for tracking successful updates)
const [value, setValue, rateLimiter] = useRateLimitedState(
0,
{ limit: 5, window: 60000, windowType: 'sliding' },
(state) => ({ executionCount: state.executionCount })
);
// Opt-in to re-render when rejection count changes (optimized for tracking rate limit violations)
const [value, setValue, rateLimiter] = useRateLimitedState(
0,
{ limit: 5, window: 60000, windowType: 'sliding' },
(state) => ({ rejectionCount: state.rejectionCount })
);
// Opt-in to re-render when execution times change (optimized for window calculations)
const [value, setValue, rateLimiter] = useRateLimitedState(
0,
{ limit: 5, window: 60000, windowType: 'sliding' },
(state) => ({ executionTimes: state.executionTimes })
);
// With rejection callback and fixed window
const [value, setValue] = useRateLimitedState(0, {
limit: 3,
window: 5000,
windowType: 'fixed',
onReject: (rateLimiter) => {
alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
}
});
// Access rateLimiter methods if needed
const handleSubmit = () => {
const remaining = rateLimiter.getRemainingInWindow();
if (remaining > 0) {
setValue(newValue);
} else {
showRateLimitWarning();
}
};
// Access the selected rate limiter state (will be empty object {} unless selector provided)
const { executionCount, rejectionCount } = rateLimiter.state;
// Default behavior - no reactive state subscriptions
const [value, setValue, rateLimiter] = useRateLimitedState(0, {
limit: 5,
window: 60000,
windowType: 'sliding'
});
// Opt-in to re-render when execution count changes (optimized for tracking successful updates)
const [value, setValue, rateLimiter] = useRateLimitedState(
0,
{ limit: 5, window: 60000, windowType: 'sliding' },
(state) => ({ executionCount: state.executionCount })
);
// Opt-in to re-render when rejection count changes (optimized for tracking rate limit violations)
const [value, setValue, rateLimiter] = useRateLimitedState(
0,
{ limit: 5, window: 60000, windowType: 'sliding' },
(state) => ({ rejectionCount: state.rejectionCount })
);
// Opt-in to re-render when execution times change (optimized for window calculations)
const [value, setValue, rateLimiter] = useRateLimitedState(
0,
{ limit: 5, window: 60000, windowType: 'sliding' },
(state) => ({ executionTimes: state.executionTimes })
);
// With rejection callback and fixed window
const [value, setValue] = useRateLimitedState(0, {
limit: 3,
window: 5000,
windowType: 'fixed',
onReject: (rateLimiter) => {
alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
}
});
// Access rateLimiter methods if needed
const handleSubmit = () => {
const remaining = rateLimiter.getRemainingInWindow();
if (remaining > 0) {
setValue(newValue);
} else {
showRateLimitWarning();
}
};
// Access the selected rate limiter state (will be empty object {} unless selector provided)
const { executionCount, rejectionCount } = rateLimiter.state;
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。