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>]
Defined in: react-pacer/src/rate-limiter/useRateLimitedState.ts:106
A React hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window. This hook combines React's useState with rate limiting functionality to provide controlled state updates.
Rate limiting is a simple "hard limit" approach - it allows all updates until the limit is reached, then blocks subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
The rate limiter supports two types of windows:
For smoother update patterns, consider:
Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.
The hook returns a tuple containing:
For more direct control over rate limiting without state management, consider using the lower-level useRateLimiter hook instead.
The hook uses TanStack Store for reactive state management via the underlying rate limiter instance. The selector parameter allows you to specify which rate limiter state changes will trigger a re-render, optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
By default, all rate limiter 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 rate limiter state properties:
• TValue
• TSelected = RateLimiterState
TValue
RateLimiterOptions<Dispatch<SetStateAction<TValue>>>
(state) => TSelected
[TValue, Dispatch<SetStateAction<TValue>>, ReactRateLimiter<Dispatch<SetStateAction<TValue>>, TSelected>]
// Basic rate limiting - update state at most 5 times per minute with a sliding window (re-renders on any rate limiter state change)
const [value, setValue, rateLimiter] = useRateLimitedState(0, {
limit: 5,
window: 60000,
windowType: 'sliding'
});
// Only 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 })
);
// Only 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 })
);
// Only 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
const { executionCount, rejectionCount } = rateLimiter.state;
// Basic rate limiting - update state at most 5 times per minute with a sliding window (re-renders on any rate limiter state change)
const [value, setValue, rateLimiter] = useRateLimitedState(0, {
limit: 5,
window: 60000,
windowType: 'sliding'
});
// Only 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 })
);
// Only 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 })
);
// Only 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
const { executionCount, rejectionCount } = rateLimiter.state;
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.