Skip to content

Commit 99773af

Browse files
Amr LabibAmr Labib
authored andcommitted
cache hooks callback functions
1 parent 588e314 commit 99773af

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

docs/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/useStopwatch.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useState, useCallback } from 'react';
22
import { Time } from './utils';
33
import { useInterval } from './hooks';
44

@@ -12,26 +12,26 @@ export default function useStopwatch({ autoStart, offsetTimestamp }) {
1212
setSeconds(passedSeconds + Time.getSecondsFromPrevTime(prevTime, true));
1313
}, isRunning ? 1000 : null);
1414

15-
function start() {
15+
const start = useCallback(() => {
1616
const newPrevTime = new Date();
1717
setPrevTime(newPrevTime);
1818
setIsRunning(true);
1919
setSeconds(passedSeconds + Time.getSecondsFromPrevTime(newPrevTime, true));
20-
}
20+
}, [passedSeconds]);
2121

22-
function pause() {
22+
const pause = useCallback(() => {
2323
setPassedSeconds(seconds);
2424
setIsRunning(false);
25-
}
25+
}, [seconds]);
2626

27-
function reset(offset = 0, newAutoStart = true) {
27+
const reset = useCallback((offset = 0, newAutoStart = true) => {
2828
const newPassedSeconds = Time.getSecondsFromExpiry(offset, true) || 0;
2929
const newPrevTime = new Date();
3030
setPrevTime(newPrevTime);
3131
setPassedSeconds(newPassedSeconds);
3232
setIsRunning(newAutoStart);
3333
setSeconds(newPassedSeconds + Time.getSecondsFromPrevTime(newPrevTime, true));
34-
}
34+
}, []);
3535

3636
return {
3737
...Time.getTimeFromSeconds(seconds), start, pause, reset, isRunning,

src/useTimer.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useState, useCallback } from 'react';
22
import { Time, Validate } from './utils';
33
import { useInterval } from './hooks';
44

@@ -20,38 +20,38 @@ export default function useTimer({ expiryTimestamp: expiry, onExpire, autoStart
2020
const [didStart, setDidStart] = useState(autoStart);
2121
const [delay, setDelay] = useState(getDelayFromExpiryTimestamp(expiryTimestamp));
2222

23-
function handleExpire() {
23+
const handleExpire = useCallback(() => {
2424
Validate.onExpire(onExpire) && onExpire();
2525
setIsRunning(false);
2626
setDelay(null);
27-
}
27+
}, [onExpire]);
2828

29-
function pause() {
29+
const pause = useCallback(() => {
3030
setIsRunning(false);
31-
}
31+
}, []);
3232

33-
function restart(newExpiryTimestamp, newAutoStart = true) {
33+
const restart = useCallback((newExpiryTimestamp, newAutoStart = true) => {
3434
setDelay(getDelayFromExpiryTimestamp(newExpiryTimestamp));
3535
setDidStart(newAutoStart);
3636
setIsRunning(newAutoStart);
3737
setExpiryTimestamp(newExpiryTimestamp);
3838
setSeconds(Time.getSecondsFromExpiry(newExpiryTimestamp));
39-
}
39+
}, []);
4040

41-
function resume() {
41+
const resume = useCallback(() => {
4242
const time = new Date();
4343
time.setMilliseconds(time.getMilliseconds() + (seconds * 1000));
4444
restart(time);
45-
}
45+
}, [seconds, restart]);
4646

47-
function start() {
47+
const start = useCallback(() => {
4848
if (didStart) {
4949
setSeconds(Time.getSecondsFromExpiry(expiryTimestamp));
5050
setIsRunning(true);
5151
} else {
5252
resume();
5353
}
54-
}
54+
}, [expiryTimestamp, didStart, resume]);
5555

5656
useInterval(() => {
5757
if (delay !== DEFAULT_DELAY) {

0 commit comments

Comments
 (0)