Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useInterval): use a memoized function #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ckknight
Copy link
Contributor

@ckknight ckknight commented Nov 2, 2018

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.

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.
@ckknight
Copy link
Contributor Author

ckknight commented Nov 2, 2018

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.

@dispix
Copy link
Owner

dispix commented Nov 3, 2018

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants