I was going through the following tutorial to implement a simple web3 app: https://learn.figment.io/tutorials/chakra-ui-with-solana-dapps#realtime-account-updates-using-polling-and-custom-react-hooks
This sets up an interval to monitor the status of an account so that it can display up to date information.
The function where the interval is set up is as follows:
function useSolanaAccount() {
const [account, setAccount] = useState(null);
const { connection } = useConnection();
const wallet = useWallet();
const init = useCallback(async () => {
if(wallet.publicKey)
console.log("usecallback publicKey:", wallet.publicKey.toString());
else
console.log("usecallback publicKey: None");
if (wallet.publicKey) {
let acc = await connection.getAccountInfo(wallet.publicKey);
setAccount(acc);
}
}, [wallet, connection]);
useEffect(() => {
if (wallet.publicKey) {
setInterval(init, 1000);
console.log("use effect publicKey:", wallet.publicKey.toString());
}
else{
console.log("clear interval");
clearInterval(init);
}
}, [init, wallet]);
return { account };
}
I've been trying to understand how to cancel the interval, and I really don't get it. From the console log it clearly reaches the code "clearInterval(init)", but it doesn't actually seem to do anything.
I've tried making init a global thing, which I read in another Stack Overflow post as a possible solution, but that didn't help.