generated from Dhaiwat10/react-library-starter
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* usePoller Hook A hook that allows the calling of a function at dedicated intervals. * Update packages/hooks/src/hooks/usePoller.ts Co-authored-by: with-heart <with.heart+git@pm.me> * Update packages/hooks/src/hooks/usePoller.ts Co-authored-by: with-heart <with.heart+git@pm.me> * Update packages/hooks/src/hooks/usePoller.ts Co-authored-by: with-heart <with.heart+git@pm.me> * Added comments regarding usage of hook * Add changeset * Add docs, story example and a slight tweak for usePoller Co-authored-by: with-heart <with.heart+git@pm.me> Co-authored-by: Dhaiwat Pandya <dhaiwatpandya@gmail.com>
- @web3-ui/hooks@0.13.0
- @web3-ui/hooks@0.12.0
- @web3-ui/hooks@0.11.1
- @web3-ui/hooks@0.11.0
- @web3-ui/hooks@0.10.0
- @web3-ui/hooks@0.9.0
- @web3-ui/hooks@0.8.1
- @web3-ui/hooks@0.8.0
- @web3-ui/core@0.8.1
- @web3-ui/core@0.8.1-next.0
- @web3-ui/core@0.8.0
- @web3-ui/core@0.7.0
- @web3-ui/core@0.6.2
- @web3-ui/core@0.6.1
- @web3-ui/core@0.6.0
- @web3-ui/core@0.5.2
- @web3-ui/core@0.5.1
- @web3-ui/core@0.5.0
- @web3-ui/core@0.4.1
- @web3-ui/core@0.4.0
- @web3-ui/core@0.3.0
- @web3-ui/core@0.2.2
- @web3-ui/components@0.13.0-next.0
- @web3-ui/components@0.12.0
- @web3-ui/components@0.11.0
- @web3-ui/components@0.10.1
- @web3-ui/components@0.10.0
- @web3-ui/components@0.9.0
- @web3-ui/components@0.8.0
- @web3-ui/components@0.7.0
- @web3-ui/components@0.6.0
- @web3-ui/components@0.5.0
1 parent
674078c
commit 000c2f8
Showing
6 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@web3-ui/hooks': minor | ||
--- | ||
|
||
A new hook usePoller has been added. This hook can be used to call a function at a certain interval repeatedly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export const usePoller = (func: () => void, delay: number): void => { | ||
/** | ||
* Calls a function at a set interval. The function will also be called | ||
* immediately. | ||
* | ||
* @param func The function to call at an interval | ||
* @param delay The delay between each interval call | ||
* @example | ||
* const callback = () => console.log('test') | ||
* usePoller(callback, 1000) // logs 'test' every second | ||
*/ | ||
|
||
const savedCbFunc = useRef(func); | ||
|
||
// Remember the latest fn. | ||
useEffect(() => { | ||
savedCbFunc.current = func; | ||
}, [func]); | ||
|
||
useEffect(() => { | ||
if (!delay) { | ||
return; | ||
} | ||
|
||
const id = setInterval(savedCbFunc.current, delay); | ||
return () => clearInterval(id); | ||
}, [delay, func]); | ||
|
||
useEffect(() => { | ||
func(); | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters