Reputation: 3958
I'm getting back to using React now after a long time of not touching it, and I am looking at the redux toolkit which I've used before, and I see RTK query.
I'm trying to understand it, and think I have, but there's one thing I'm not sure if I'm missing or it is just designed to work like that.
Do we only use the RTKQ to fetch data that we don't need cached at all? For example loading a specific post that the user clicked on? I'm asking because in any tutorial I've watched or read I haven't seen any interaction with the other slices.
If for example I need to get some information that needs to be kept (otherwise I'd just keep fetching it), then I will use the regular createAsyncThunk
?
Upvotes: 0
Views: 3424
Reputation: 67547
It's the other way around entirely.
RTK Query is only for caching fetched data.
It's always been common to fetch data from the server and put it into the Redux store, but it takes a lot of work. You have to write thunks that make the request, dispatch actions based on the results, and write reducers that manage loading status and save the cached data. Then you have to write useEffect
hooks to dispatch those thunks from components, and selectors that read the data from the store.
RTK Query does all of that work for you, automatically. And it then improves on those basics, by fetching new data when arguments change, refetching query data automatically when you send an update to the server, managing cache lifetimes and removing old data when components no longer need it, streaming updates, and much much more.
I'd recommend going through the relevant sections of the official "Redux Essentials" tutorial in our docs, as well as the RTKQ usage guides:
Upvotes: 9