You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am on my own Web3 project. I decided to use swr to cache responses to prevent unnecessary reloads.
The problem is contract methods are called too much even though I put same parameters as key in useSWR.
Example code is here.
import{useAccount}from"./use-account-local-module"importuseSWRfrom"swr"import{useMemo}from"react"importcreateContractfrom"./some-local-module-file"constuseBalance=()=>{constprovider=useProvider()constuserAddr=useAccount()constcontract=useMemo(()=>{if(provider===null||provider===undefined){return}if(userAddr===null||userAddr===undefined){return}returncreateContract(provider,userAddr)},[provider,address])constfetchKey=contract&&userAddr ? [contract,userAddr]asconst : undefinedconst{data: balance,mutate: fetchBalance}=useSWR(fetchKey,([contract,address])=>{console.log("contract method are called now")returncontract.getBalance(provider,address)})return[balance,fetchBalance]asconst}
The function createContract returns a ethers contract from one provider and one user wallet address. By calling getBalance, I could get a BigNumber balue.
When I call this hook with several components, the message contract method are called now is printed depending on the number of components.
As I know, swr is able to save data to reuse. But it seems caching are not working with any reasons.
The last thing I found is the objects contract or provider can not be serialized by JSON.stringify. Is that related with this issue? I think swr can not find cached data.
When corrected, How can I make swr and ethers compatible?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am on my own Web3 project. I decided to use
swr
to cache responses to prevent unnecessary reloads.The problem is contract methods are called too much even though I put same parameters as key in
useSWR
.Example code is here.
The function
createContract
returns a ethers contract from one provider and one user wallet address. By callinggetBalance
, I could get a BigNumber balue.When I call this hook with several components, the message
contract method are called now
is printed depending on the number of components.As I know,
swr
is able to save data to reuse. But it seems caching are not working with any reasons.The last thing I found is the objects
contract
orprovider
can not be serialized byJSON.stringify
. Is that related with this issue? I thinkswr
can not find cached data.When corrected, How can I make
swr
andethers
compatible?Beta Was this translation helpful? Give feedback.
All reactions