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
Copy file name to clipboardexpand all lines: docs/tutorials/essentials/part-3-data-flow.md
+36-16
Original file line number
Diff line number
Diff line change
@@ -107,21 +107,37 @@ We'll start by adding a `src/app/store.ts` file and creating the store.
107
107
108
108
```ts title="src/app/store.ts"
109
109
import { configureStore } from'@reduxjs/toolkit'
110
+
importtype { Action } from'@reduxjs/toolkit'
111
+
112
+
interfaceCounterState {
113
+
value:number
114
+
}
115
+
116
+
// An example slice reducer function that shows how a Redux reducer works inside.
117
+
// We'll replace this soon with real app logic.
118
+
function counterReducer(state:CounterState= { value: 0 }, action:Action) {
119
+
switch (action.type) {
120
+
// Handle actions here
121
+
default: {
122
+
returnstate
123
+
}
124
+
}
125
+
}
110
126
111
127
// highlight-start
112
128
exportconst store =configureStore({
113
129
// Pass in the root reducer setup as the `reducer` argument
114
130
reducer: {
115
-
//An example slice reducer function that returns a fixed state value
116
-
value: (state:number=123) =>state
131
+
//Declare that `state.counter` will be updated by the `counterReducer` function
132
+
counter: counterReducer
117
133
}
118
134
})
119
135
// highlight-end
120
136
```
121
137
122
138
**`configureStore` always requires a `reducer` option**. This should typically be an object containing the individual "slice reducers" for the different parts of the application. (If necessary, you can also create the root reducer function separately and pass that as the `reducer` argument.)
123
139
124
-
For this first step, we're passing in a mock slice reducer function for the `value` field, to show what the setup looks like. We'll replace this with a real slice reducer in just a minute.
140
+
For this first step, we're passing in a mock slice reducer function for the `counter` slice, to show what the setup looks like. We'll replace this with a real slice reducer for the actual app we want to build in just a minute.
125
141
126
142
:::tip Setup with Next.js
127
143
@@ -173,17 +189,21 @@ Now that we have a store, we can use the Redux DevTools extension to view the cu
173
189
174
190
If you open up your browser's DevTools view (such as by right-clicking anywhere in the page and choosing "Inspect"), you can click on the "Redux" tab. This will show the history of dispatched actions and the current state value:
The current state value should be an object that looks like this:
179
197
180
198
```ts
181
199
{
182
-
value: 123
200
+
counter: {
201
+
value: 123
202
+
}
183
203
}
184
204
```
185
205
186
-
That shape was defined by the `reducer` option we passed into `configureStore`: an object, with a field named `value`, and the slice reducer for the `value` field returns a number as its state.
206
+
That shape was defined by the `reducer` option we passed into `configureStore`: an object, with a field named `counter`, and the slice reducer for the `counter` field returns an object like `{value}` as its state.
187
207
188
208
### Exporting Store Types
189
209
@@ -194,11 +214,11 @@ We need to export those types from the `store.ts` file. We'll define the types b
194
214
```ts title="src/app/store.ts"
195
215
import { configureStore } from'@reduxjs/toolkit'
196
216
217
+
// omit counter slice setup
218
+
197
219
exportconst store =configureStore({
198
-
// Pass in the root reducer setup as the `reducer` argument
199
220
reducer: {
200
-
// An example slice reducer function that returns a fixed state value
201
-
value: (state:number=123) =>state
221
+
counter: counterReducer
202
222
}
203
223
})
204
224
@@ -212,7 +232,7 @@ export type RootState = ReturnType<typeof store.getState>
212
232
// highlight-end
213
233
```
214
234
215
-
If you hover over the `RootState` type in your editor, you should see `typeRootState = { value:number; }`. Since this type is automatically derived from the store definition, all the future changes to the `reducer` setup will automatically be reflected in the `RootState` type as well. This way we only need to define it once, and it will always be accurate.
235
+
If you hover over the `RootState` type in your editor, you should see `typeRootState = { counter:CounterState; }`. Since this type is automatically derived from the store definition, all the future changes to the `reducer` setup will automatically be reflected in the `RootState` type as well. This way we only need to define it once, and it will always be accurate.
Every time we create a new slice, we need to add its reducer function to our Redux store. We already have a Redux store being created, but right now it doesn't have any data inside. Open up `app/store.ts`, import the `postsReducer` function, and update the call to `configureStore` so that the `postsReducer` is being passed as a reducer field named `posts`:
306
+
Every time we create a new slice, we need to add its reducer function to our Redux store. We already have a Redux store being created, but right now it doesn't have any data inside. Open up `app/store.ts`, import the `postsReducer` function, remove all of the `counter` code, and update the call to `configureStore` so that the `postsReducer` is being passed as a reducer field named `posts`:
287
307
288
308
```ts title="app/store.ts"
289
309
import { configureStore } from'@reduxjs/toolkit'
290
310
311
+
// highlight-next-line
312
+
// Removed the `counterReducer` function, `CounterState` type, and `Action` import
@@ -307,7 +330,7 @@ We can confirm that this works by opening the Redux DevTools Extension and looki
307
330
308
331
### Showing the Posts List
309
332
310
-
Now that we have some posts data in our store, we can create a React component that shows the list of posts. All of the code related to our feed posts feature should go in the `posts` folder, so go ahead and create a new file named `PostsList.tsx` in there.
333
+
Now that we have some posts data in our store, we can create a React component that shows the list of posts. All of the code related to our feed posts feature should go in the `posts` folder, so go ahead and create a new file named `PostsList.tsx` in there. (Note that since this is a React component written in TypeScript and using JSX syntax, it needs a `.tsx` file extension for TypeScript to compile it properly)
311
334
312
335
If we're going to render a list of posts, we need to get the data from somewhere. React components can read data from the Redux store using the `useSelector` hook from the React-Redux library. The "selector functions" that you write will be called with the entire Redux `state` object as a parameter, and should return the specific data that this component needs from the store.
313
336
@@ -647,11 +670,8 @@ Let's recap what you've learned in this section:
0 commit comments