Skip to content

Commit 82955c3

Browse files
committed
fix: switch to useStateWithDeps
1 parent 77789ec commit 82955c3

5 files changed

Lines changed: 34 additions & 49 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
},
5858
"dependencies": {
5959
"prop-types": "^15.7.2",
60-
"time-sync": "^2.2.5"
60+
"time-sync": "^2.2.5",
61+
"use-state-with-deps": "^1.0.0"
6162
}
6263
}

src/use-countdown.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useContext, useEffect, useRef, useDebugValue } from "react";
1+
import { useContext, useEffect, useDebugValue } from "react";
22
import TimeContext from "./context";
33
import { Interval } from "time-sync/constants";
4-
import { useForceUpdate } from "./use-force-update";
4+
import { useStateWithDeps } from "use-state-with-deps";
55

66
export interface PartialCountdownConfig {
77
until?: number;
@@ -25,32 +25,23 @@ export function useCountdown(
2525
countdownConfig: PartialCountdownConfig = {}
2626
): number {
2727
const timeSync = useContext(TimeContext);
28-
const forceUpdate = useForceUpdate();
28+
const usableConfig = getUsableConfig(countdownConfig);
2929

30-
const lastConfig = useRef(countdownConfig);
31-
let usableConfig = getUsableConfig(lastConfig.current);
32-
33-
const timeLeft = useRef(timeSync.getTimeLeft(usableConfig));
34-
35-
if (
36-
countdownConfig.interval !== lastConfig.current.interval ||
37-
countdownConfig.until !== lastConfig.current.until
38-
) {
39-
lastConfig.current = countdownConfig;
40-
usableConfig = getUsableConfig(countdownConfig);
41-
42-
timeLeft.current = timeSync.getTimeLeft(usableConfig);
43-
}
30+
const [timeLeft, setTimeLeft] = useStateWithDeps(() => {
31+
return timeSync.getTimeLeft({
32+
interval: usableConfig.interval,
33+
until: usableConfig.until
34+
});
35+
}, [usableConfig.interval, usableConfig.until]);
4436

4537
useEffect((): (() => void) | void => {
4638
if (Date.now() < usableConfig.until) {
4739
return timeSync.createCountdown((newTimeLeft): void => {
48-
timeLeft.current = newTimeLeft;
49-
forceUpdate();
40+
setTimeLeft(newTimeLeft);
5041
}, usableConfig);
5142
}
5243
}, [usableConfig.until, usableConfig.interval]);
5344

54-
useDebugValue(timeLeft.current);
55-
return timeLeft.current;
45+
useDebugValue(timeLeft);
46+
return timeLeft;
5647
}

src/use-force-update.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/use-time.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useContext, useDebugValue, useEffect, useRef } from "react";
1+
import { useContext, useDebugValue, useEffect } from "react";
22
import TimeContext from "./context";
33
import { Interval } from "time-sync/constants";
4-
import { useForceUpdate } from "./use-force-update";
4+
import { useStateWithDeps } from "use-state-with-deps";
55

66
export interface TimerConfig {
77
interval?: Interval;
@@ -10,29 +10,23 @@ export interface TimerConfig {
1010

1111
export function useTime(timerConfig: TimerConfig = {}): number {
1212
const timeSync = useContext(TimeContext);
13-
const forceUpdate = useForceUpdate();
14-
15-
const lastConfig = useRef(timerConfig);
16-
const time = useRef(timeSync.getCurrentTime(timerConfig));
17-
18-
if (
19-
timerConfig.interval !== lastConfig.current.interval ||
20-
timerConfig.unit !== lastConfig.current.unit
21-
) {
22-
lastConfig.current = timerConfig;
23-
24-
time.current = timeSync.getCurrentTime(timerConfig);
25-
}
13+
const [time, setTime] = useStateWithDeps(
14+
() =>
15+
timeSync.getCurrentTime({
16+
interval: timerConfig.interval,
17+
unit: timerConfig.unit
18+
}),
19+
[timerConfig.interval, timerConfig.unit]
20+
);
2621

2722
useEffect(
2823
(): (() => void) =>
2924
timeSync.addTimer((currentTime): void => {
30-
time.current = currentTime;
31-
forceUpdate();
32-
}, lastConfig.current),
33-
[lastConfig.current.unit, lastConfig.current.interval]
25+
setTime(currentTime);
26+
}, timerConfig),
27+
[timerConfig.unit, timerConfig.interval]
3428
);
3529

36-
useDebugValue(time.current);
37-
return time.current;
30+
useDebugValue(time);
31+
return time;
3832
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5655,6 +5655,11 @@ urix@^0.1.0:
56555655
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"
56565656
integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI=
56575657

5658+
use-state-with-deps@^1.0.0:
5659+
version "1.0.0"
5660+
resolved "https://registry.yarnpkg.com/use-state-with-deps/-/use-state-with-deps-1.0.0.tgz#670e6f98849db63741875ee8a7979f1b8a02fe4a"
5661+
integrity sha512-EIIDxPzlU7hVhMmyY8XNq+ASMfhzoG7W3oNcwM1f8fRnHt2cQH+8vNr8jTat7aLLv56rOaMFjvKi/5tDIJLNwA==
5662+
56585663
use@^3.1.0:
56595664
version "3.1.1"
56605665
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"

0 commit comments

Comments
 (0)