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
+export type AppThunk = ThunkAction<void, RootState, null, Action<string>
534
532
```
535
533
536
-
The `AppThunk` type declares that:
534
+
The `AppThunk` type declares that the "action" that we're using is specifically a thunk function. The thunk is customized with some additional type parameters:
537
535
538
-
- This function is an "action creator" (which takes some arguments, and returns a specific action type)
539
-
- The "action" that we're returning is specifically a thunk function. The thunk is customized with some additional type parameters:
540
-
1. Return value: the thunk doesn't return anything
541
-
2. State type for `getState`: returns our `RootState` type
542
-
3. "Extra argument": the thunk middleware can be customized to pass in an extra value, but we aren't doing that in this app
543
-
4. Action types accepted by `dispatch`: any action whose `type` is a string.
536
+
1. Return value: the thunk doesn't return anything
537
+
2. State type for `getState`: returns our `RootState` type
538
+
3. "Extra argument": the thunk middleware can be customized to pass in an extra value, but we aren't doing that in this app
539
+
4. Action types accepted by `dispatch`: any action whose `type` is a string.
544
540
545
541
There are many cases where you would want different type settings here, but these are probably the most common settings. This way, we can avoid repeating that same type declaration every time we write a thunk.
546
542
547
543
#### Adding the Repo Details Slice
548
544
549
545
Now that we have that type, we can write a slice of state for fetching details on a repo.
550
546
551
-
> -[Add a slice for storing repo details](https://github.com/markerikson/rsk-github-issues-example/commit/f4d5a1840028b645c600eecc3f8297e5cc6a6b93)
547
+
> -[Add a slice for storing repo details](https://github.com/markerikson/rsk-github-issues-example/commit/da9291bf428a96c3f2e8862f42e3be08461d514c)
552
548
553
549
**features/repoSearch/repoDetailsSlice.ts**
554
550
@@ -591,10 +587,10 @@ export const {
591
587
592
588
exportdefaultrepoDetails.reducer
593
589
594
-
exportconst fetchIssuesCount:AppThunk= (
590
+
exportconst fetchIssuesCount = (
595
591
org:string,
596
592
repo:string
597
-
) =>asyncdispatch=> {
593
+
): AppThunk=>asyncdispatch=> {
598
594
try {
599
595
const repoDetails =awaitgetRepoDetails(org, repo)
600
596
dispatch(getRepoDetailsSuccess(repoDetails))
@@ -609,7 +605,7 @@ The first part of this should look straightforward. We declare our slice state s
609
605
Down at the bottom, we have our first data fetching thunk. The important things to notice here are:
610
606
611
607
-**The thunk is defined separately from the slice**, since RSK currently has no special syntax for defining thunks as part of a slice.
612
-
-**We declare the thunk action creator as an arrow function, and use the `AppThunk` type we just created.** You can use either arrow functions or the `function` keyword to write thunk functions, but because we defined the `AppThunk` type as "an action creator that returns a thunk function", we need to apply that type to the action creator function. TS won't currently allow us to declare a type for a function declared with the `function` keyword, so we use an arrow function. (If we changed `AppThunk` to be just the "thunk function" part, we could write `function fetchIssuesCount() : AppThunk`as the return value instead.)
608
+
-**We declare the thunk action creator as an arrow function, and use the `AppThunk` type we just created.** You can use either arrow functions or the `function` keyword to write thunk functions and thunk action creators, so we could also have written this as `function fetchIssueCount() : AppThunk` instead.
613
609
-**We use the `async/await` syntax for the thunk function itself.** Again, this isn't required, but `async/await` usually results in simpler code than nested Promise `.then()` chains.
614
610
-**Inside the thunk, we dispatch the plain action creators that were generated by the `createSlice` call**.
615
611
@@ -619,7 +615,7 @@ While not shown, we also add the slice reducer to our root reducer.
619
615
620
616
Now that the repo details slice exists, we can use it in the `<IssuesListPage>` component.
621
617
622
-
> -[Update IssuesListPage to fetch repo details via Redux](https://github.com/markerikson/rsk-github-issues-example/commit/798a9bcf38eef1a1e5857057352c7b9e0a20cbcd)
618
+
> -[Update IssuesListPage to fetch repo details via Redux](https://github.com/markerikson/rsk-github-issues-example/commit/964134a00bc1a54ba8758ca274049c9174e88f9a)
623
619
624
620
**features/issuesList/IssuesListPage.tsx**
625
621
@@ -698,7 +694,7 @@ Inside our `useEffect`, we drop the `fetchIssueCount` function, and dispatch `fe
698
694
699
695
Next up, we need to replace the logic for fetching a list of open issues.
700
696
701
-
> -[Add a slice for tracking issues state](https://github.com/markerikson/rsk-github-issues-example/commit/43679930bfec13a6105337b5d8733980e200ceb5)
697
+
> -[Add a slice for tracking issues state](https://github.com/markerikson/rsk-github-issues-example/commit/b2e5919651a5076e3857da96321bc979a8ae54b9)
@@ -815,7 +811,7 @@ This slice is a bit longer, but it's the same basic approach as before: write th
815
811
816
812
Now we can finish converting the `<IssuesListPage>` component by swapping out the issues fetching logic.
817
813
818
-
> -[Update IssuesListPage to fetch issues data via Redux](https://github.com/markerikson/rsk-github-issues-example/commit/87ee62d93cff3b8686ee5a774a389e681423ab01)
814
+
> -[Update IssuesListPage to fetch issues data via Redux](https://github.com/markerikson/rsk-github-issues-example/commit/8dbdc0726ccecf354a01351786196648c752c0a6)
819
815
820
816
Let's look at the changes.
821
817
@@ -954,7 +950,7 @@ It's very similar to `<IssuesListPage>`. We store the current displayed `Issue`,
954
950
955
951
We conveniently already have the Redux logic for fetching a single issue - we wrote that already as part of `issuesSlice.ts`. So, we can immediately jump straight to using that here in `<IssueDetailsPage>`.
956
952
957
-
> -[Update IssueDetailsPage to fetch issue data via Redux](https://github.com/markerikson/rsk-github-issues-example/commit/a57fe3f97ccb26ee2c481c2f71057a58c7fd4dad)
953
+
> -[Update IssueDetailsPage to fetch issue data via Redux](https://github.com/markerikson/rsk-github-issues-example/commit/46bcddbe1078574fab649a13f61a6bf3d0f42839)
958
954
959
955
**features/issueDetails/IssueDetailsPage.tsx**
960
956
@@ -1019,7 +1015,7 @@ Interestingly, there's actually a bit of a change in behavior here. The original
1019
1015
1020
1016
We have one more slice left to write - we need to fetch and store comments for the current issue.
1021
1017
1022
-
> -[Add a slice for tracking comments data](https://github.com/markerikson/rsk-github-issues-example/commit/e765f09b44a75cb99b3e2866e3ff2806a533620f)
1018
+
> -[Add a slice for tracking comments data](https://github.com/markerikson/rsk-github-issues-example/commit/46bcddbe1078574fab649a13f61a6bf3d0f42839)
@@ -1091,7 +1087,7 @@ The slice should look pretty familiar at this point. Our main bit of state is a
1091
1087
1092
1088
The final step is to swap the comments fetching logic in `<IssueDetailsPage>`.
1093
1089
1094
-
> -[Update IssueDetailsPage to fetch comments via Redux](https://github.com/markerikson/rsk-github-issues-example/commit/0977da34631088981f58f9d160573d83d4225238)
1090
+
> -[Update IssueDetailsPage to fetch comments via Redux](https://github.com/markerikson/rsk-github-issues-example/commit/9d1246a4d89f21da1f0e5377f040bc766e1fc0fd)
1095
1091
1096
1092
**features/issueDetails/IssueDetailsPage.tsx**
1097
1093
@@ -1177,6 +1173,6 @@ Hopefully you now have a solid understand of how Redux Starter Kit looks in a re
1177
1173
1178
1174
Let's wrap this up with one more look at the complete source code and the running app:
0 commit comments