Skip to content

Commit

Permalink
fix: doble execution of onPause on useCountDown and useCountUp #33
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcastillop committed Apr 6, 2024
1 parent 7998458 commit c0874bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useStopwatch } from "./hooks";
import { useCountDown } from "./hooks";

const App = () => {
const { current, isPaused, isOver, pause, play, reset, togglePause } =
useStopwatch();
useCountDown(1, 10);

return (
<div>
Expand Down
10 changes: 7 additions & 3 deletions src/helpers/useCounter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from "react";
import { useState, useEffect, useRef } from "react";
import { BaseCounter, BaseCounterOptions } from "../interfaces";
import { addLeadingZero } from ".";

Expand All @@ -16,6 +16,7 @@ export const useCounter = (
const [count, setCount] = useState(isCountingUp ? min : max);
const [paused, setPaused] = useState(startPaused ?? false);
const [isOver, setIsOver] = useState(false);
const wasPausedRef = useRef(startPaused ?? false);

useEffect(() => {
if (paused) {
Expand All @@ -42,8 +43,11 @@ export const useCounter = (
}, [isOver]);

useEffect(() => {
paused && onPause && onPause();
}, [paused]);
if (!wasPausedRef.current && paused) {
onPause && onPause();
}
wasPausedRef.current = paused;
}, [paused, onPause]);

return {
current: {
Expand Down

0 comments on commit c0874bd

Please sign in to comment.