Skip to content

Commit 3c26c05

Browse files
authored
Documentation Updates: RootState, AppDispatch, misc. (#516)
* Add note about inferring RootState, useAppDispatch hook example, minor fixes * Add note to advanced tutorial about inferring RootState
1 parent 846971b commit 3c26c05

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

docs/api/createAsyncThunk.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ interface RejectedAction<ThunkArg> {
151151
requestId: string
152152
arg: ThunkArg
153153
aborted: boolean
154-
condition: bolean
154+
condition: boolean
155155
}
156156
}
157157

docs/api/createEntityAdapter.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The methods generated by `createEntityAdapter` will all manipulate an "entity st
3535

3636
Sample usage:
3737

38-
```js
38+
```ts
3939
import {
4040
createEntityAdapter,
4141
createSlice,
@@ -71,13 +71,15 @@ const store = configureStore({
7171
}
7272
})
7373

74-
type State = ReturnType<typeof store.getState>
74+
type RootState = ReturnType<typeof store.getState>
7575

7676
console.log(store.getState().books)
77-
// {ids: [], entities: {} }
77+
// { ids: [], entities: {} }
7878

7979
// Can create a set of memoized selectors based on the location of this entity state
80-
const booksSelectors = booksAdapter.getSelectors<State>(state => state.books)
80+
const booksSelectors = booksAdapter.getSelectors<RootState>(
81+
state => state.books
82+
)
8183

8284
// And then use the selectors to retrieve values
8385
const allBooks = booksSelectors.selectAll(store.getState())

docs/tutorials/advanced-tutorial.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export type RootState = ReturnType<typeof rootReducer>
8585
export default rootReducer
8686
```
8787

88+
> **Note:** For other ways to infer the `RootState`, view the [Usage with TypeScript](../usage/usage-with-typescript#getting-the-state-type) guide
89+
8890
#### Store Setup and HMR
8991

9092
Next, we'll create the store instance, including hot-reloading the root reducer. By using the [`module.hot` API for reloading](https://webpack.js.org/concepts/hot-module-replacement/), we can re-import the new version of the root reducer function whenever it's been recompiled, and tell the store to use the new version instead.

docs/usage/usage-with-typescript.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,34 @@ const rootReducer = combineReducers({})
2828
export type RootState = ReturnType<typeof rootReducer>
2929
```
3030
31+
Alternatively, if you choose to not create a `rootReducer` yourself and instead pass the slice reducers directly to `configureStore()`, you need to slightly modify the typing to correctly infer the root reducer:
32+
33+
```ts
34+
import { configureStore } from '@reduxjs/toolkit'
35+
// ...
36+
const store = configureStore({
37+
reducer: {
38+
one: oneSlice.reducer,
39+
two: twoSlice.reducer
40+
}
41+
})
42+
export type RootState = ReturnType<typeof store.getState>
43+
```
44+
3145
### Getting the `Dispatch` type
3246
33-
If you want to get the `Dispatch` type from your store, you can extract it after creating the store.
34-
It is recommend to give the type a different name like `AppDispatch` to prevent confusion, as the type name `Dispatch` is usually overused.
47+
If you want to get the `Dispatch` type from your store, you can extract it after creating the store. It is recommended to give the type a different name like `AppDispatch` to prevent confusion, as the type name `Dispatch` is usually overused. You may also find it to be more convenient to export a hook like `useAppDispatch` shown below, then using it wherever you'd call `useDispatch`.
3548
3649
```typescript {6}
3750
import { configureStore } from '@reduxjs/toolkit'
3851
import rootReducer from './rootReducer'
52+
3953
const store = configureStore({
4054
reducer: rootReducer
4155
})
56+
4257
export type AppDispatch = typeof store.dispatch
58+
export const useAppDispatch = () => useDispatch<AppDispatch>() // Export a hook that can be resused to resolve types
4359
```
4460

4561
### Correct typings for the `Dispatch` type

0 commit comments

Comments
 (0)