Skip to content

useInterval delay of null with immediate still runs function #128

@vincerubinetti

Description

@vincerubinetti

I have a situation where I want to run a useInterval function immediately when rendering the hook, but not when its delay is null.

const shouldRun = false;

useInterval(
  () => {
    // without this, func will run once because of `immediate`, as of v6.0.0
    if (!shouldRun) return;
    // ...
  },
  shouldRun ? 50 : null,
  // need func to run immediately (not after 50 ms), but only if shouldRun is true
  { immediate: true },
);

As it currently works, immediate will make the func run once even when delay is null. I guess a user could want it this way, but in my opinion, this is unexpected behavior.

It's not a big deal since I can just check shouldRun at the top of the function and early exit, but I'd still bet that most people would be caught off guard by this.

Related: #129


A side note, the controls option could be better documented as "Whether to control interval manually with returned start/stop functions instead of provided delay".

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