I'm using SWR to show a paginated component, very similar to the example in the docs. My key looks something like
useSWR(`/api/items?offset=${offset}`)
I would like to upload a new list of items, and I want to tell SWR to revalidate all items requests since they will no longer be valid. I'm assuming mutate
is still the right avenue to do this, but is there a way that I can tell mutate to invalidate many keys at once? Or ever better, all keys that match /api/items(.*)
?
Thank you!
If you are using SWR 2 or more, You can do something like this:
import { mutate } from 'swr'
// Or from the hook if you customized the cache provider:
// { mutate } = useSWRConfig()
mutate(
key => typeof key === 'string' && key.startsWith('/api/items?offset')
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With