T
TanStack8mo ago
sensitive-blue

Query/page Update following mutation

I have a component that uses injectQuery to fetch some data. I assign the .data() part to a computed signal.
query = injectQuery(queryName(value));
results = computed(() => this.query.data());
query = injectQuery(queryName(value));
results = computed(() => this.query.data());
This component has a child component (which contains a form) which accepts an input signal - assigned to the results variable using [param]='results()' When I update the value of the form and save the data in the parent component, I invalidate the query but my form does not update - it reverts to the old data. I can see in DevTools Network and Tanstack Devtool that the query was refetched and has the correct data. How do I make the page show the correct data??
7 Replies
underlying-yellow
underlying-yellow8mo ago
Why don’t you just pass query.data() to the child, is there any reason that you just don’t do that instead of adding an abstraction layer with the computed signal? Do the new data are different than the previous ones? If I’m not mistaken, Angular query uses structural sharing to avoid re-rendering, so if the data are the same structurally, the data signal isn’t updated.
sensitive-blue
sensitive-blueOP8mo ago
I'll take a look. I am also using NgRX SignalStore for storing component level data rather than passing lots of data around between components/sub-components. I am wondering if it is possible to store the query in the store instead of each component.
underlying-yellow
underlying-yellow8mo ago
Mhh, I don’t really know NgRX SignalStore so I don’t know. Personally, I define my queries in the components and I define the options in services dedicated to that.
protestant-coral
protestant-coral8mo ago
At my current project I just return computeds with queryOptions from Signal Store. The functions for the queryOptions are simple methods on an Angular service. And this is also a good approach, just pass signals from signal store to the query. In any case when using both TanStack Query and Signal Store, avoid syncing the query data to Signal Store. It would complicate code a lot and syncing server state to client state would remove a lot of benefits gained from TanStack Query.
underlying-yellow
underlying-yellow8mo ago
I must say I do update manually the queries cache after I perform mutations. I do not invalidate queries. I prefer this approach since you get "instant updates" and this is more energy efficient since you don’t have to perform extra queries after performing a mutation but it does complicate the code for sure. I usually have 3 services that works together, a service which performs the http requests, a service which updates the cache of queries and a service which store the queries options. Those 3 are then "duplicated" for each different usage that I need.
protestant-coral
protestant-coral8mo ago
I must say I do update manually the queries cache after I perform mutations. I do not invalidate queries. I prefer this approach since you get "instant updates
Perfectly valid approach too
sensitive-blue
sensitive-blueOP8mo ago
@arnoud I think I am on the right track but would appreciate a small code snippet that illustrates your point and also shows how you are using mutations with your store.

Did you find this page helpful?