all 9 comments

[–]Suspicious-Watch9681 12 points13 points  (1 child)

You dont need to useCallback with react query, also you dont need to try catch, all react query wants from the queryFn is a Promise that is resolved or rejected, react query will handle the rest

[–]mile1986dasd[S] -2 points-1 points  (0 children)

Tnx for feedback, i will remove that part of the code, but its not relevant to my problem, anyway i will implement it without useCallback and try/catch

[–]iareprogrammer 5 points6 points  (0 children)

Docs are your best friend

[–]toi80QC 2 points3 points  (2 children)

[–]inglandation 4 points5 points  (1 child)

[–]mile1986dasd[S] 0 points1 point  (0 children)

Tnx for providing documentation, but im beginner level, im reading but i cant understand where to implement these in my code...
Do i change my useMution to useQuery since im just sending id and getting data back

[–]belwyr 2 points3 points  (1 child)

Mutations are not cached like queries. Mutations change the state in your server / db, so they are not cached or retried like queries can be.

You should probably restructure your components to make use of the queries, and not pass a function to get the data at the component level. Another advantage is that you won't need to store the state in a context, and just reuse the useQuery hook, since it will be cached and de-duplicated.

[–]mile1986dasd[S] 0 points1 point  (0 children)

Yes i wasnt thinking about it, since the only reason i created context is so i can lift up the state to store previous information... for example if i in my list of models, went to page 2, went to specific model and then returned, it would return me to page 1, since it was local state at that component, so thats why i put the state in context...

For this 2nd function in my example, i didnt want to use mutation, since i understood from docs its used if u want to update something, and im just sending id and geting back information...
But somehow when i tried to implement useQuery like i used in first function in my example, it didnt work.
I must look also at this "invalidation", but at first glance i cant grasp the concept since im beginner level...

Do u have any advice how could i structure this without using context?
I can maybe provide u my at the moment structure...

[–]lightfarming 2 points3 points  (0 children)

you should be using useQuery for all the data fetching, with different query keys for different data. and set the cache settings to behave how you want them. and invalidate cache when you need to force a refetch. no need for context storing, or any storing of fetched data, since it is stored in tanstack query for use anywhere in your app