react-pirate
react-pirate copied to clipboard
fix(useInterval): use a memoized function
By memoizing the function via useCallback within useInterval, coupled with the explicit cache of [] (meaning that the effect only has an onMount and onUnmount), only a single interval is kept for the lifecycle of the component, that will be appropriately called with updated props/state.
I believe a similar approach should be taken with useTimeout, but it's quite viable that one might want to be able to "reset" a timeout such that within a single component's lifecycle, the timeout might fire more than just X ms after mount. It's also possible in that case that one might want to use a different cache than either [] or undefined.
I ran some tests in a sandbox and both behaviours are not quite perfect. I tried by pushing the memFn into the array of useEffect and it seems to work better : https://codesandbox.io/s/olzln0z4oq
What do you think ?