function useRateLimitedCallback<TFn, TArgs>(fn, options): (...args) => boolean
function useRateLimitedCallback<TFn, TArgs>(fn, options): (...args) => boolean
Defined in: react-pacer/src/rate-limiter/useRateLimitedCallback.ts:51
A React hook that creates a rate-limited version of a callback function. This hook is essentially a wrapper around the basic rateLimiter function that is exported from @tanstack/pacer, but optimized for React with reactive options and a stable function reference.
Rate limiting is a simple "hard limit" approach - it allows all calls until the limit is reached, then blocks subsequent calls until the window resets. Unlike throttling or debouncing, it does not attempt to space out or intelligently collapse calls. This can lead to bursts of rapid executions followed by periods where all calls are blocked.
For smoother execution patterns, consider:
Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits or other scenarios requiring hard caps on execution frequency.
This hook provides a simpler API compared to useRateLimiter, making it ideal for basic rate limiting needs. However, it does not expose the underlying RateLimiter instance.
For advanced usage requiring features like:
Consider using the useRateLimiter hook instead.
• TFn extends (...args) => any
• TArgs extends any[]
TFn
RateLimiterOptions
Function
...TArgs
boolean
// Rate limit API calls to maximum 5 calls per minute
const makeApiCall = useRateLimitedCallback(
(data: ApiData) => {
return fetch('/api/endpoint', { method: 'POST', body: JSON.stringify(data) });
},
{
limit: 5,
window: 60000, // 1 minute
onReject: () => {
console.warn('API rate limit reached. Please wait before trying again.');
}
}
);
// Rate limit API calls to maximum 5 calls per minute
const makeApiCall = useRateLimitedCallback(
(data: ApiData) => {
return fetch('/api/endpoint', { method: 'POST', body: JSON.stringify(data) });
},
{
limit: 5,
window: 60000, // 1 minute
onReject: () => {
console.warn('API rate limit reached. Please wait before trying again.');
}
}
);
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.