Skip to content

Commit 000c2f8

Browse files
Ibby-devvwith-heartDhaiwat10
authored
Add usePoller Hook (#168)
* 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>
1 parent 674078c commit 000c2f8

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
lines changed

.changeset/grumpy-windows-retire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web3-ui/hooks': minor
3+
---
4+
5+
A new hook usePoller has been added. This hook can be used to call a function at a certain interval repeatedly

packages/hooks/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The following hooks are available:
3535
- [useTokenBalance](#usetokenbalance)
3636
- [useReadOnlyContract](#usereadonlycontract)
3737
- [useReadOnlyProvider](#usereadonlyprovider)
38+
- [usePoller](#usepoller)
3839

3940
---
4041

@@ -170,3 +171,15 @@ const provider = useReadOnlyProvider(
170171
'https://rinkeby.infura.io/v3/YOUR_INFURA_ID'
171172
);
172173
```
174+
175+
---
176+
177+
### usePoller
178+
179+
The `usePoller` hook takes in a function and a delay in milliseconds. It will call the function in every delay.
180+
181+
```tsx
182+
import { usePoller } from '@web3-ui/hooks';
183+
184+
usePoller(() => console.log(contract.balanceOf(account)), 1000); // will log the balance every second
185+
```

packages/hooks/src/hooks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { useTransaction } from './useTransaction';
44
export { useTokenBalance } from './useTokenBalance';
55
export { useReadOnlyProvider } from './useReadOnlyProvider';
66
export { useReadOnlyContract } from './useReadOnlyContract';
7+
export { usePoller } from './usePoller';

packages/hooks/src/hooks/usePoller.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useEffect, useRef } from 'react';
2+
3+
export const usePoller = (func: () => void, delay: number): void => {
4+
/**
5+
* Calls a function at a set interval. The function will also be called
6+
* immediately.
7+
*
8+
* @param func The function to call at an interval
9+
* @param delay The delay between each interval call
10+
* @example
11+
* const callback = () => console.log('test')
12+
* usePoller(callback, 1000) // logs 'test' every second
13+
*/
14+
15+
const savedCbFunc = useRef(func);
16+
17+
// Remember the latest fn.
18+
useEffect(() => {
19+
savedCbFunc.current = func;
20+
}, [func]);
21+
22+
useEffect(() => {
23+
if (!delay) {
24+
return;
25+
}
26+
27+
const id = setInterval(savedCbFunc.current, delay);
28+
return () => clearInterval(id);
29+
}, [delay, func]);
30+
31+
useEffect(() => {
32+
func();
33+
}, []);
34+
};

packages/hooks/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export {
44
useTokenBalance,
55
useTransaction,
66
useReadOnlyProvider,
7-
useReadOnlyContract
7+
useReadOnlyContract,
8+
usePoller
89
} from './hooks';
910
export { Provider, Web3Context } from './Provider';
1011
export { NETWORKS, CHAIN_ID_TO_NETWORK } from './constants';

packages/hooks/src/stories/UseContract.stories.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { storiesOf } from '@storybook/react';
22
import React, { useEffect } from 'react';
3-
import { Provider, useWallet, useContract, NETWORKS } from '..';
3+
import { Provider, useWallet, useContract, NETWORKS, usePoller } from '..';
44
import { Button, Input, Divider, VStack } from '@chakra-ui/react';
55
import { ethers } from 'ethers';
66
import { useReadOnlyContract } from '../hooks/useReadOnlyContract';
@@ -68,6 +68,15 @@ const Default = () => {
6868
amount: '0'
6969
});
7070

71+
usePoller(async () => {
72+
try {
73+
const greeting = await contract.greet();
74+
console.log(greeting); // logs the greeting every second
75+
} catch (error) {
76+
console.log(error);
77+
}
78+
}, 1000);
79+
7180
const handleGreet = async () => alert(await contract.greet());
7281
const handleChangeState = (stateName: string) => ({ target: { value } }) => {
7382
setState({ ...state, [stateName]: value });

0 commit comments

Comments
 (0)