From d13671b75dac85b1f5cd6ed8841525099c9c03d6 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Mon, 22 Aug 2022 16:26:24 -0300 Subject: [PATCH] Improve docs --- src/useLatestBlock.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/useLatestBlock.ts b/src/useLatestBlock.ts index dc77e69..ba5e21a 100644 --- a/src/useLatestBlock.ts +++ b/src/useLatestBlock.ts @@ -36,6 +36,11 @@ export const useLatestBlock = (provider?: JsonRpcProvider) => { return latestBlock; }; +/** + * Returns the latest block number AND hook an internal listener + * that'll update and trigger a component render as a side effect + * every time it is notified of a new block by the web3 provider. + */ export const useLatestBlockNumber = (provider?: JsonRpcProvider) => { const [latestBlock, setLatestBlock] = useState(); @@ -44,12 +49,15 @@ export const useLatestBlockNumber = (provider?: JsonRpcProvider) => { return; } + // Immediately read and set the latest block number const readLatestBlock = async () => { const blockNum = await provider.getBlockNumber(); setLatestBlock(blockNum); }; readLatestBlock(); + // Hook a listener that'll update the latest block number + // every time it is notified of a new block const listener = async (blockNumber: number) => { setLatestBlock(blockNumber); };