The function to be warmed up. It should accept an optional argument
and handle a special warmup call with { __warmup: true }.
Configuration options for the warmup behavior.
void
immediate option is enabled, the function will be warmed up immediately
using requestIdleCallback if available, or a setTimeout fallback.import { useFunctionWarmup } from './use-function-warmup';
const myFunction = (arg?: any) => {
if (arg?.__warmup) {
console.log('Warming up...');
} else {
console.log('Executing function...');
}
};
function MyComponent() {
useFunctionWarmup(myFunction, { intervalMs: 60000, immediate: true });
return <div>My Component</div>;
}
A React hook that periodically invokes a given function to keep it "warmed up." This can be useful for functions that require initialization or caching to improve performance during actual usage.