Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Please add partial matching for mutation keys #761

Closed
nimabrownlee opened this issue Nov 11, 2020 · 11 comments
Closed

Feature request: Please add partial matching for mutation keys #761

nimabrownlee opened this issue Nov 11, 2020 · 11 comments
Labels
area: cache documentation Improvements or additions to documentation feature request New feature or request

Comments

@nimabrownlee
Copy link

Feature request

Description / Observed Behavior

My previous issue was completely removed for some strange reason (not even closed) so I am opening a new one. Right now if you want to use the global mutate function you have to provide a complete match with the key. It's very weird and even react-query supports partial matching. Please add it as its very crucial. I cant replicate every key in every component.

@markprins
Copy link

I have a similar situation. I'm using multiple SWRInfinites for page handling and in various configurations which hold different datasets. When certain data is updated, some of these should revalidate their data.

It would be nice to revalidate/invalidate partial keys like mutate('/toplevel/nested/*').

@shuding shuding added the feature request New feature or request label Nov 18, 2020
@DaniGuardiola
Copy link

Same! In my case I PATCH a specific item (e.g. PATCH /user/<id>) that I want to refresh on a different view with all items and pagination (e.g. GET /user?offset=20&limit=20).

@RaddishIoW
Copy link

There is a swr-mutate-many package that might help.

@nimabrownlee
Copy link
Author

@RaddishIoW Hey, how do you use it?

I tried mutateMany('**/tasks/*', undefined, true) but it removes the task list. How can I make it just re-fetch?

@diar-kourehpaz
Copy link

@nimabrownlee Here is another way to mutate keys matching a regex:

import { mutate, cache } from 'swr'
      const pattern = /tasks/
      cache
        .keys()
        .filter((key) => pattern.test(key))
        .forEach((key) => mutate(key))

@RaddishIoW
Copy link

RaddishIoW commented Jun 10, 2021

@nimabrownlee

I tried mutateMany('**/tasks/*', undefined, true) but it removes the task list. How can I make it just re-fetch?

Hmmm... All I do to force a re-fetch is to call mutateMany(<key>). I don't pass anything but the key.

@huozhi huozhi added the documentation Improvements or additions to documentation label Jun 11, 2021
@huozhi
Copy link
Member

huozhi commented Jun 11, 2021

We're working on the doc for swr@beta to land the new cache API to support this feature. The current exposed cache in swr@0.x.x is not documented and highly risky to use.

There's a example section for partial mutation in that PR, feel free to give a try with swr@beta, look forward to the feedbacks.

@huozhi
Copy link
Member

huozhi commented Jun 17, 2021

Docuement and examples are landed in cache docs

@huozhi huozhi closed this as completed Jun 17, 2021
@ungarida
Copy link

The above link is out of date, this is the new one https://swr.vercel.app/docs/advanced/cache

@rjborba
Copy link

rjborba commented Aug 1, 2022

What about composite keys?
Is there a recommendation for something like:

useSwr({url, token})?

@utenma
Copy link

utenma commented Oct 15, 2023

examples #1946

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cache documentation Improvements or additions to documentation feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants