Creating query keys using complex objects (e.g., class instances) #6945
Unanswered
RolginRoman
asked this question in
Q&A
Replies: 1 comment
-
|
you need to provide your own |
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.
-
Hello team,
I'm trying to figure out best practices for using complex objects (eg. class instances) inside
useQueryEnv:
Objective:
How to use correctly any objects provided, for instance, via React Context that are more complex that POJO.
For instance, class instances.
These instances may or may not have some circular dependencies, that break
JSON.stringify(obviously, but still disappointing that JSON.stringify can't do it)😀TL;DR
how to use DIContext instance that can be of completely unexpected form (circular deps for instance) in query
Example
codesandbox example: https://codesandbox.io/p/sandbox/query-keys-with-complex-deps-f5f4gp?file=%2Fsrc%2FQueryUse.tsx%3A14%2C37
Here main points are:
ExternalService.ts- some class that is instantiated inside the App context (usually an app-wide service, can be 3rd party, so not much freedom here regarding its usage and creation).UseQuery.tsx- useQuery is used in this component. It is created based on some primitive input (eg.modein my example) and anExternalServiceinstance.The instance of the class is provided to
useQueryviaReact.Contextonly after clicking onInit servicebutton.Until that useQuery is waiting (expected), but after an instantiation - useQuery won't refetch value (expected again). However,
my question is lying here, how to inject/use/apply this new instance and make useQuery work.
Additional info
I see a WA: implement stable serialization by hand (even trying to achieve it in
App.tsx-decycledStringifyfn), but looks extremely fragile 😔.I assume, this discussion can be related to the old one here #1829, but maybe anyone has developed an approach around this
Beta Was this translation helpful? Give feedback.
All reactions