aboutlo / ether-swr

Ether-SWR is a React hook that fetches Ethereum data. It streamlines the chores to keep the internal state of the Decentralized App (DApp), batches the RPC calls to an Ethereum node and cache the responses
MIT License
138 stars 12 forks source link

Interact with Ethereum methods (e.g. getBalance) #8

Closed lidachao111222 closed 3 years ago

lidachao111222 commented 3 years ago

Hi, Lorenzo. Sorry for disturbing you.

I have one question about the Interact with Ethereum methods when I use the getBalance function with ether-swr. When I tried the minimal example. I saw that the ethereum amount won't change if I switch to another network(ex: from Ropsten to Rinkeby).

Would you like to give me some ideas please? Thank you.

aboutlo commented 3 years ago

I haven't tested the minimal example with network switch but I can imagine two things:

import cache from 'swr' 
cache.clear()
lidachao111222 commented 3 years ago

Thanks for your reply.

The first idea that I tried before. It doesn't work.

I use the second idea like this: useEffect(() => { if (library !== undefined) { cache.clear() } }, [chainId]);

It also doesn't work.

Finally, I used another way to get balance and it works! lol
Here is the code:

```const { data: balance, mutate, error } = useEtherSWR(...``` // change different network if (error) { window.location.reload(); }

The page will reload if I change the network. Although I need to connect the wallet again.

aboutlo commented 3 years ago

Have a look at how it works https://github.com/NoahZinsmeister/web3-react. The problem you are facing seems more related to the web3 provider than ether-swr itself.