Skip to content

Commit

Permalink
✨ Wrap some methods with useCallback (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliencrn authored Mar 5, 2024
1 parent 9b65ce8 commit d42741f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,13 @@
"avatar_url": "https://avatars.githubusercontent.com/u/3635023?v=4",
"profile": "https://github.com/misidoro",
"contributions": ["code"]
},
{
"login": "gromchen",
"name": "Yuriy Gromchenko",
"avatar_url": "https://avatars.githubusercontent.com/u/828918?v=4",
"profile": "https://crowds.space/",
"contributions": ["code"]
}
],
"contributorsPerLine": 7,
Expand Down
5 changes: 5 additions & 0 deletions .changeset/metal-guests-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"usehooks-ts": patch
---

Wrap `useCountdown` methods with `useCallback` (from #326 by @gromchen)
5 changes: 5 additions & 0 deletions .changeset/perfect-pianos-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"usehooks-ts": patch
---

Wrap `useCounter` methods with `useCallback` (from #326 by @gromchen)
1 change: 1 addition & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ Big thanks go to all our contributors! [[Become a contributor](https://github.co
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kyrylo-soulandwolf"><img src="https://avatars.githubusercontent.com/u/54762253?v=4?s=64" width="64px;" alt="kyrylo-soulandwolf"/><br /><sub><b>kyrylo-soulandwolf</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=kyrylo-soulandwolf" title="Code">💻</a> <a href="https://github.com/juliencrn/usehooks-ts/issues?q=author%3Akyrylo-soulandwolf" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/misidoro"><img src="https://avatars.githubusercontent.com/u/3635023?v=4?s=64" width="64px;" alt="Miguel Isidoro"/><br /><sub><b>Miguel Isidoro</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=misidoro" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://crowds.space/"><img src="https://avatars.githubusercontent.com/u/828918?v=4?s=64" width="64px;" alt="Yuriy Gromchenko"/><br /><sub><b>Yuriy Gromchenko</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=gromchen" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[![License](https://badgen.net/badge/License/MIT/blue)](https://github.com/juliencrn/usehooks-ts/blob/master/LICENSE)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/usehooks-ts)
![npm](https://img.shields.io/npm/v/usehooks-ts)<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-240-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-241-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

<br />
Expand Down Expand Up @@ -428,6 +428,7 @@ Big thanks go to all our contributors! [[Become a contributor](https://github.co
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kyrylo-soulandwolf"><img src="https://avatars.githubusercontent.com/u/54762253?v=4?s=64" width="64px;" alt="kyrylo-soulandwolf"/><br /><sub><b>kyrylo-soulandwolf</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=kyrylo-soulandwolf" title="Code">💻</a> <a href="https://github.com/juliencrn/usehooks-ts/issues?q=author%3Akyrylo-soulandwolf" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/misidoro"><img src="https://avatars.githubusercontent.com/u/3635023?v=4?s=64" width="64px;" alt="Miguel Isidoro"/><br /><sub><b>Miguel Isidoro</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=misidoro" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://crowds.space/"><img src="https://avatars.githubusercontent.com/u/828918?v=4?s=64" width="64px;" alt="Yuriy Gromchenko"/><br /><sub><b>Yuriy Gromchenko</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=gromchen" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
3 changes: 2 additions & 1 deletion packages/usehooks-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[![License](https://badgen.net/badge/License/MIT/blue)](https://github.com/juliencrn/usehooks-ts/blob/master/LICENSE)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/usehooks-ts)
![npm](https://img.shields.io/npm/v/usehooks-ts)<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-240-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-241-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

<br />
Expand Down Expand Up @@ -428,6 +428,7 @@ Big thanks go to all our contributors! [[Become a contributor](https://github.co
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kyrylo-soulandwolf"><img src="https://avatars.githubusercontent.com/u/54762253?v=4?s=64" width="64px;" alt="kyrylo-soulandwolf"/><br /><sub><b>kyrylo-soulandwolf</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=kyrylo-soulandwolf" title="Code">💻</a> <a href="https://github.com/juliencrn/usehooks-ts/issues?q=author%3Akyrylo-soulandwolf" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/misidoro"><img src="https://avatars.githubusercontent.com/u/3635023?v=4?s=64" width="64px;" alt="Miguel Isidoro"/><br /><sub><b>Miguel Isidoro</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=misidoro" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://crowds.space/"><img src="https://avatars.githubusercontent.com/u/828918?v=4?s=64" width="64px;" alt="Yuriy Gromchenko"/><br /><sub><b>Yuriy Gromchenko</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=gromchen" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
4 changes: 2 additions & 2 deletions packages/usehooks-ts/src/useCountdown/useCountdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ export function useCountdown(
/**
* Will set running false and reset the seconds to initial value
*/
const resetCountdown = () => {
const resetCountdown = useCallback(() => {
stopCountdown()
resetCounter()
}
}, [stopCountdown, resetCounter])

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

import type { Dispatch, SetStateAction } from 'react'

Expand Down Expand Up @@ -32,15 +32,17 @@ interface UseCounterOutput {
export function useCounter(initialValue?: number): UseCounterOutput {
const [count, setCount] = useState(initialValue ?? 0)

const increment = () => {
const increment = useCallback(() => {
setCount(x => x + 1)
}
const decrement = () => {
}, [])

const decrement = useCallback(() => {
setCount(x => x - 1)
}
const reset = () => {
}, [])

const reset = useCallback(() => {
setCount(initialValue ?? 0)
}
}, [initialValue])

return {
count,
Expand Down

0 comments on commit d42741f

Please sign in to comment.