-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Open
Description
useTimeoutFn
, starts the timeout automatically on mount. In our case, we needed something similar but fully manual which is a timeout only starts when we explicitly trigger it (e.g., after an API success).
Why this might be useful
- No automatic timeout on mount
- Full control:
start()
,cancel()
,reset()
- Ability to reuse the last delay (
reset
) - Useful in callbacks, controlled UI flows, etc.
API
const [isReady, cancel, start, reset] = useTimeoutControlledFn(fn, defaultMs?: number);
isReady()
→ returnstrue | false | null
cancel()
→ cancels the current timeoutstart(delay?)
→ starts a new timeoutreset()
→ reuses last delay
Example
const [isReady, cancel, start, reset] = useTimeoutControlledFn(() => {
console.log('Executed after delay!');
}, 1500);
// Manually start after a successful API call
const onApiSuccess = () => start();
If this sounds useful, I’m happy to prepare a full PR with tests and docs. Just wanted to get your feedback first
muradsofi
Metadata
Metadata
Assignees
Labels
No labels