4

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!

JuanCaicedo
  • 3,132
  • 2
  • 14
  • 36

1 Answers1

1

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')
)
sina
  • 98
  • 6