-
Notifications
You must be signed in to change notification settings - Fork 226
Improve use timeout #1306
Improve use timeout #1306
Conversation
it('continues to respect the delay after the callback changes', () => { | ||
const {callback, wrapper} = setup({delay: ONE_SECOND}); | ||
|
||
expect(spy).not.toHaveBeenCalled(); | ||
expect(newSpy).toHaveBeenCalled(); | ||
timer.runTimersToTime(HALF_A_SECOND); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
const newCallback = jest.fn(); | ||
wrapper.setProps({callback: newCallback}); | ||
|
||
timer.runTimersToTime(HALF_A_SECOND); | ||
expect(newCallback).toHaveBeenCalled(); | ||
expect(callback).not.toHaveBeenCalled(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this should be a breaking change or not. Technically, it does break the previous behaviour, though it would be weird behaviour to depend on, so I'm leaning towards breaking
, but I want to confirm with reviewers.
Previously
Changing the callback
prop causes the timeout to be reset, effectively restarting the timer. If the timeout has already fired, a new timeout will be set, and the new callback will be run (in addition to the one that already ran).
Now
Changing the callback
does nothing. When the timeout fires, it will run whatever the current timeout is. If the timeout has already fired, nothing will happen, unless the delay
is also changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda see this as a bugfix tbh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌 I've updated the Changelog to include two entries.
- Added support for
null
delay
- Fixed changing callback resetting the timeout
ca68568
to
8b6cafb
Compare
Tagging in Web Foundation for review as I'm tied up in stuff. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me, I kinda think this should be positioned as a bugfix. I don't think the original behaviour was intended, as even the old test seemed worded in such a way that I would assume it would work the way it does now.
Thanks for the review @TheMallen! I'm ok with the change to |
8b6cafb
to
651018b
Compare
@sambostock makes sense to me! |
7aad94c
to
bd06f6b
Compare
bd06f6b
to
b670692
Compare
Description
This updates the
useTimeout
hook inshopify/react-hooks
to support the same API and behaviour as theuseInterval
hook added in #1241.Specifically, it changes the behaviour of
callback
: Previously, any time this prop changes, the timeout is reset. Now, thecallback
is free to change any time before the timeout fires, since thecallback
is stored in a ref.delay
:null
can now be passed to clear the timeout.Type of change
react-hooks
Minor: The change todelay
acceptingnull
, in addition to numbers, as well as making changing the callback not reset the timeout.Checklist
useInterval
React Hook #1241 was merged