react-timer-hook
react-timer-hook copied to clipboard
#31 - memoize returned callbacks for useTimer
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.
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)
@amrlabib could you please review and merge this?
Has this been abandoned? Is anyone still merging PRs? If not, fork??
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 ?
Done in v3.0.6