xnimorz / use-debounce

A debounce hook for react

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Infinite startTimer loop causing high CPU load

gtanczyk opened this issue · comments

Hello!

I'm not able to give exact reproduction steps, but this screenshot demonstrates the problem:
image

I'm using useDebouncedCallback in a regular component. The component itself is not re-rendered, and there is nothing that could call the callback itself.

Version of React: 16.9.46

Thanks for help!

commented

Hi @gtanczyk
I'd like to ask which arguments were provided to the component?
It seems the problem is connected to shouldInvoke func https://github.com/xnimorz/use-debounce/blob/master/src/useDebouncedCallback.ts#L135-L153, but I need a little bit more info to understand

I'm observing this behavior in many different instances of useDebouncedCallback, but this is the most common setup:

const debouncedFunc = useDebouncedCallback(func, 500, { maxWait: 1000 });

func is not invoked, shouldInvoke is returning false, because this hook is unmounted. (mounted.current === false)

Unfortunately I'm not able to provide reliable reproduction steps.

@xnimorz Please have a look at this example: https://codesandbox.io/s/determined-antonelli-4lhw6?file=/src/App.js
so looks like it's a problem of unmounted components.

seeing a similar issue.

const saveScroll = useDebouncedCallback((scrollOffset: number) => {
    //do stuff
  }, 200);

<List
            ref={listRef}
            itemKey={getItemKey}
            className="AvailabilitySearch-destinationResultList AvailabilitySearch-destinationResultList--simple"
            innerElementType="ul"
            height={height}
            itemCount={results?.offeringAvailability.length ?? 0}
            width={width}
            itemSize={getItemSize}
            onScroll={({ scrollOffset }) => saveScroll.callback(scrollOffset)}
          >
            {Row}
          </List>

after a little bit of scrolling, the tab spikes the cpu.

commented

Hi @kelly-tock, could you provide a little bit more information? cause, I won't be able to reproduce the error.

commented

@gtanczyk Thank you for the information! I fixed it in use-debounce@5.2.1

commented

I'm closing this issue at the moment, as the problem with the unmounted component is gone. If you still have any problems with infinite timeouts, please feel free to create a new issue or reopen this one

@xnimorz Thanks a lot