Skip to content

Hook Proposal: useTimeoutControlledFn — a manual version of useTimeoutFn #2645

@muradsofi

Description

@muradsofi

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() → returns true | false | null
  • cancel() → cancels the current timeout
  • start(delay?) → starts a new timeout
  • reset() → 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions