How can I update a collection from different locations referencing the same queryKey? #823
Unanswered
TotiCuervo
asked this question in
Q&A
Replies: 1 comment
-
|
Don't use a hook to create your collection. Your collections should be defined top level in memory (basically outside of any React component). Note: this is the pattern for query clients with both Tanstack Query and Tanstack DB as well since they need to be global so that caching can be coordinated globally. So your collection is defined at the top level and then used with the live query hooks to bring them into the React world. |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
I have abstracted a collection to a hook for reusability. I have a page, PageA, that uses that collection via a useLiveQuery. Nested in PageA is a component, ComponentB, where I would like to be able to manipulate the collection. My expectation is that I could call up and manipulate my collection and it would persist both in PageA and ComponentB. So far it seems like Collections are not global to the rest of the app but are isolated to the place where it is initialized. I am expected my change in ComponentB to have impacted all todos at the queryKey 'todos'.
I am trying to update a collection in a place separate from where the useLiveQuery is. I expected Collections to be similar to useQuery where I can reference the same queryKey in different components, make changes, and those changes would persist throughout the other places the query is used.
Is there something that I am missing in my understanding or am I just doing something wrong? I could prop drill the function I want to use, but that feels like it defeats the point.
Hook:
PageA
ComponentB
Beta Was this translation helpful? Give feedback.
All reactions