react-timer-hook icon indicating copy to clipboard operation
react-timer-hook copied to clipboard

#31 - memoize returned callbacks for useTimer

Open dylanmrowe opened this issue 4 years ago • 2 comments

Tested by running/verifying the demo site locally. This will allow users to use the returned callbacks (start, pause, resume, restart) as dependencies of useEffect, useCallback, etc. without having the dependency change on every single render.

Should be fully backwards compatible with existing uses of useTimer.

Only implemented for useTimer for now. This was mainly to demonstrate the principle. I can do the others if required.

dylanmrowe avatar Dec 30 '20 22:12 dylanmrowe

In testing this altered code further, using 'start' as a dependency of useEffect still causes infinite renders when 'restart' is also called inside that useEffect. This is due to restart() forcing the start() callback to update (by modifying the expiryTimestamp), which re-triggers the useEffect. However there are ways to work around this.

There's also a bug where programmatically calling pause() right after restart() doesn't pause the timer. Putting a timeout of 50ms or so on the pause lets it work. restart() does some state updates that need to finish before pause() will do something. (That doesn't have anything to do with this PR and probably needs a separate issue)

dylanmrowe avatar Jan 14 '21 19:01 dylanmrowe

@amrlabib could you please review and merge this?

chybisov avatar May 02 '22 10:05 chybisov

Has this been abandoned? Is anyone still merging PRs? If not, fork??

ysageev avatar Sep 30 '22 19:09 ysageev

Sorry for not maintaining the library. i would like to update the library and add memoized callbacks to all hooks let me know if you have time to resolve conflicts and make this PR ready anytime soon so that we can merge. Also question why we memoize onExpire ?

amrlabib avatar Apr 30 '23 16:04 amrlabib

Done in v3.0.6

amrlabib avatar May 13 '23 23:05 amrlabib