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

Use callback for countdown reset #326

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/useCountdown/useCountdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ function useCountdown(
/**
* Will set running false and reset the seconds to initial value
*/
const resetCountdown = () => {
const resetCountdown = useCallback(() => {
stopCountdown()
resetCounter()
}
}, [resetCounter, stopCountdown])

const countdownCallback = useCallback(() => {
if (count === countStop) {
Expand Down
8 changes: 4 additions & 4 deletions src/useCounter/useCounter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dispatch, SetStateAction, useState } from 'react'
import { Dispatch, SetStateAction, useCallback, useState } from 'react'

interface UseCounterOutput {
count: number
Expand All @@ -11,9 +11,9 @@ interface UseCounterOutput {
function useCounter(initialValue?: number): UseCounterOutput {
const [count, setCount] = useState(initialValue || 0)

const increment = () => setCount(x => x + 1)
const decrement = () => setCount(x => x - 1)
const reset = () => setCount(initialValue || 0)
const increment = useCallback(() => setCount(x => x + 1), [])
const decrement = useCallback(() => setCount(x => x - 1), [])
const reset = useCallback(() => setCount(initialValue || 0), [initialValue])

return {
count,
Expand Down