has this library built-in cache? #40
-
Hey guys, sorry if this is a noob question, but im wondering if theres already caching built in like in swr hooks . |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
No, not (yet?) – we are currently only using the state for response. BUT, you could connect swr and wagmi to get the best of both worlds. Since swr is accepting promises, you could easily use the const ComponentName = () => {
// example from docs: https://wagmi-xyz.vercel.app/docs/hooks/useContractRead#usage
const [_, read] = useContractRead(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'getHunger',
)
// example from swr docs: https://swr.vercel.app/docs/data-fetching#fetch
const { data } = useSWR('some-unique-id', read)
// compoent here...
} or you could go even further and create your own custom hook, if you need the caching on multiple positions: const useCachedContractRead = () => {
// example from docs: https://wagmi-xyz.vercel.app/docs/hooks/useContractRead#usage
const [_, read] = useContractRead(
{
addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
contractInterface: wagmigotchiABI,
},
'getHunger',
)
// example from swr docs: https://swr.vercel.app/docs/data-fetching#fetch
return useSWR('some-unique-id', read)
} And then you can just use the hook like before with |
Beta Was this translation helpful? Give feedback.
No, not (yet?) – we are currently only using the state for response. BUT, you could connect swr and wagmi to get the best of both worlds. Since swr is accepting promises, you could easily use the
read
function, which is being returned by theuseContractRead
and let swr cache it inside it's system.