Should I put RTK Query results into the store?
Asked Answered
C

1

9

Trying to understand RTK Query and how to use it optimally in a React app.

I have a Search component in one part of my application. When a search is executed, I want the results to appear in another area, far away in the component tree.

All the examples I've seen in the docs immediately returns the rendered result which isn't possible if the Search and Result components are not co-located. Is it bad practice or missing the point of RTK Query if I parse the result to the store?

Curia answered 2/8, 2021 at 6:48 Comment(0)
C
10

The result is already in the store, the question is more how to access it.

The easiest way would be to just fire the same query hook in your second component. If that query requires an argument, you should probably move that argument into the store and have both components access it to retrieve the store value, instead of duplicating the cache value outside your cache.

As for "why" you should do it that way: as long as data is only in the cache, RTKQ has control over it - including stuff like cleaning it up if it is not required any more. Once you copy data out, you have to do all that stuff by hand.

Creedon answered 2/8, 2021 at 7:41 Comment(1)
Thank you! The point about moving search arguments into the store was the missing piece for how to make it work!Curia

© 2022 - 2024 — McMap. All rights reserved.