You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -599,7 +600,8 @@ You can't have a "social media" app if there aren't any other people involved! L
599
600
Since the concept of "users" is different than the concept of "posts", we want to keep the code and data for the users separated from the code and data for posts. We'll add a new `features/users` folder, and put a `usersSlice` file in there. Like with the posts slice, for now we'll add some initial entries so that we have data to work with.
@@ -926,7 +928,8 @@ Then, we can define a new reducer that will handle updating the reaction count f
926
928
Like with editing posts, we need to know the ID of the post, and which reaction button the user clicked on. We'll have our `action.payload` be an object that looks like `{id, reaction}`. The reducer can then find the right post object, and update the correct reactions field.
Finally, it also doesn't make sense to allow the current user to edit posts defined by _other_ users. We can update the `<SinglePostPage>` to only show an "Edit Post" button if the post author ID matches the current user ID:
@@ -1436,7 +1441,8 @@ You can chain these together, like `builder.addCase().addCase().addMatcher().add
1436
1441
Given that, we can import the `userLoggedOut` action from `authSlice.ts` into `postsSlice.ts`, listen for that action inside of `postsSlice.extraReducers`, and return an empty posts array to reset the posts list on logout:
@@ -408,7 +409,8 @@ Redux Toolkit's `createAsyncThunk` API generates thunks that automatically dispa
408
409
Let's start by adding a thunk that will make an HTTP request to retrieve a list of posts. We'll import the `client` utility from the `src/api` folder, and use that to make a request to `'/fakeApi/posts'`.
@@ -891,7 +889,8 @@ This is because the post entries are being randomly generated by the fake API se
891
889
Like last time, we'll create another async thunk to get the users from the API and return them, then handle the `fulfilled` action in the `extraReducers` slice field. We'll skip worrying about loading state for now:
@@ -1052,7 +1054,7 @@ We're going to need to import `createEntityAdapter`, create an instance that has
1052
1054
import {
1053
1055
// highlight-start
1054
1056
createEntityAdapter,
1055
-
EntityState
1057
+
type EntityState
1056
1058
// highlight-end
1057
1059
// omit other imports
1058
1060
} from'@reduxjs/toolkit'
@@ -1504,18 +1506,17 @@ Now we can go add a listener that will watch for the `addNewPost.fulfilled` acti
1504
1506
There's [multiple approaches we can use for defining listeners in our codebase](https://redux-toolkit.js.org/api/createListenerMiddleware#organizing-listeners-in-files). That said, it's usually a good practice to define listeners in whatever slice file seems most related to the logic we want to add. In this case, we want to show a toast when a post gets added, so let's add this listener in the `postsSlice` file:
Copy file name to clipboardExpand all lines: docs/tutorials/essentials/part-7-rtk-query-basics.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,10 +26,10 @@ import { DetailedExplanation } from '../../components/DetailedExplanation'
26
26
If you prefer a video course, you can [watch this RTK Query video course by Lenz Weber-Tronic, the creator of RTK Query, for free at Egghead](https://egghead.io/courses/rtk-query-basics-query-endpoints-data-flow-and-typescript-57ea3c43?af=7pnhj6) or take a look at the first lesson right here:
@@ -260,11 +260,16 @@ Fortunately, this is simple to fix. RTK Query actually uses `createAsyncThunk` i
260
260
Currently, the toast listener is watching for the single specific action type with `actionCreator: addNewPost.fulfilled`. We'll update it to watch for the posts being added with `matcher: apiSlice.endpoints.addNewPost.matchFulfilled`:
@@ -951,7 +959,7 @@ Similar to `onQueryStarted`, `onCacheEntryAdded` receives two parameters. The fi
951
959
There's also two additional Promises that can be waited on:
952
960
953
961
-`cacheDataLoaded`: resolves with the first cached value received, and is typically used to wait for an actual value to be in the cache before doing more logic
954
-
- `cacheEntryRemoved`: resolves when this cache entry is removed (i.e, there are no more subscribers and the cache entry has been garbage-collected)
962
+
-`cacheEntryRemoved`: resolves when this cache entry is removed (i.e, there are no more subscribers and the cache entry has been garbage-collected)
955
963
956
964
As long as 1+ subscribers for the data are still active, the cache entry is kept alive. When the number of subscribers goes to 0 and the cache lifetime timer expires, the cache entry will be removed, and `cacheEntryRemoved` will resolve. Typically, the usage pattern is:
Copy file name to clipboardExpand all lines: docs/tutorials/typescript.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,7 +92,8 @@ All generated actions should be defined using the `PayloadAction<T>` type from R
92
92
You can safely import the `RootState` type from the store file here. It's a circular import, but the TypeScript compiler can correctly handle that for types. This may be needed for use cases like writing selector functions.
0 commit comments