How to normalize cache to only use a single key when there are multiple keys? #5636
Unanswered
JeremyBernier
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
My
useQuery
is calling an API (/posts/:idOrSlug
where each resource can be identified by either one of two keys - an ID or a slug. Initially the frontend only has the slug but not the ID, but theuseQuery()
network call returns the ID.How do I make it so that the react-query cache 1. stores each item only once (instead of duplicating for slug and ID), and 2. preferably uses just the ID as the primary key (since ID is immutable and the slug could theoretically change)?
Essentially I want the key from
useQuery()
to change from slug to ID. Originally I was going to manually update the cache by adding anonSuccess
touseQuery()
, but that callback is getting deprecated (and I imagine there must be a cleaner way anyways).To elaborate more on my specific use case - imagine a blogging application where the frontend initially only has the post slug from the URL but not the post ID, but calling
GET /posts/:idOrSlug
returns the post ID. I'm not exactly sure what to set thequeryKey
in theuseQuery()
to get this desired functionality, again since I initially don't know what the ID is when callinguseQuery
.(also asked on StackOverflow: https://stackoverflow.com/questions/76563755/how-to-normalize-react-query-cache-to-only-use-a-single-key-when-there-are-multi)
Beta Was this translation helpful? Give feedback.
All reactions