Skip to content

Commit 04b6878

Browse files
authored
Minor fixes in migrating to modern redux documentation (#4494)
1 parent f503238 commit 04b6878

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

docs/usage/migrating-to-modern-redux.mdx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Redux has been around since 2015, and our recommended patterns for writing Redux
2020

2121
Many users are working on older Redux codebases that have been around since before these "modern Redux" patterns existed. Migrating those codebases to today's recommended modern Redux patterns will result in codebases that are much smaller and easier to maintain.
2222

23-
The good news is that **you can migrate your code to modern Redux incrementally, piece by piece, with old and new Redux code coexisting and working together!**.
23+
The good news is that **you can migrate your code to modern Redux incrementally, piece by piece, with old and new Redux code coexisting and working together!**
2424

2525
This page covers the general approaches and techniques you can use to modernize an existing legacy Redux codebase.
2626

@@ -150,7 +150,7 @@ import usersReducer from '../features/users/usersSlice'
150150
import { api } from '../features/api/apiSlice'
151151
import { serviceLayer } from '../features/api/serviceLayer'
152152

153-
import sanitizeStateForDevtools from './devtools'
153+
import stateSanitizerForDevtools from './devtools'
154154
import customMiddleware from './someCustomMiddleware'
155155

156156
// Can call `combineReducers` yourself if needed
@@ -168,7 +168,6 @@ const persistConfig = {
168168

169169
const persistedReducer = persistReducer(persistConfig, rootReducer)
170170

171-
172171
const store = configureStore({
173172
// Can create a root reducer separately and pass that in
174173
reducer: rootReducer,
@@ -192,8 +191,8 @@ const store = configureStore({
192191
return middleware
193192
},
194193
// Turn off devtools in prod, or pass options in dev
195-
devTools: process.env.NODE_ENV == 'production' ? false {
196-
sanitizeState: sanitizeStateForDevtools
194+
devTools: process.env.NODE_ENV === 'production' ? false : {
195+
stateSanitizer: stateSanitizerForDevtools
197196
}
198197
})
199198
```
@@ -256,7 +255,7 @@ export default function todosReducer(state = initialState, action) {
256255
}
257256
```
258257

259-
**Redux Toolkit's `createSlice` API was designed to eliminate all the "boilerplate" with writing reducers, actions, and immutable updates!**.
258+
**Redux Toolkit's `createSlice` API was designed to eliminate all the "boilerplate" with writing reducers, actions, and immutable updates!**
260259

261260
With Redux Toolkit, there's multiple changes to that legacy code:
262261

@@ -284,7 +283,7 @@ const todosSlice = createSlice({
284283
todoAdded(state, action) {
285284
const { id, text } = action.payload
286285
// "Mutating" update syntax thanks to Immer, and no `return` needed
287-
todos.push({
286+
state.todos.push({
288287
id,
289288
text,
290289
completed: false
@@ -295,7 +294,7 @@ const todosSlice = createSlice({
295294
// Look for the specific nested object to update.
296295
// In this case, `action.payload` is the default field in the action,
297296
// and can hold the `id` value - no need for `action.id` separately
298-
const matchingTodo = state.find(todo => todo.id === action.payload)
297+
const matchingTodo = state.todos.find(todo => todo.id === action.payload)
299298

300299
if (matchingTodo) {
301300
// Can directly "mutate" the nested object
@@ -449,7 +448,7 @@ import {
449448
fetchTodosStarted,
450449
fetchTodosSucceeded,
451450
fetchTodosFailed
452-
} from '../actions/todos
451+
} from '../actions/todos'
453452

454453
// Saga to actually fetch data
455454
export function* fetchTodos() {
@@ -705,7 +704,7 @@ const pingSlice = createSlice({
705704
}
706705
})
707706
708-
export const { ping } = pingSlice.actions
707+
export const { pong } = pingSlice.actions
709708
export default pingSlice.reducer
710709
711710
// highlight-start
@@ -714,7 +713,7 @@ export default pingSlice.reducer
714713
// it directly in a slice file.
715714
startListening({
716715
// Match this exact action type based on the action creator
717-
actionCreator: ping,
716+
actionCreator: pong,
718717
// Run this effect callback whenever that action is dispatched
719718
effect: async (action, listenerApi) => {
720719
// Listener effect functions get a `listenerApi` object
@@ -812,7 +811,7 @@ import { CounterState } from '../reducers/counter'
812811
813812
// omit reducer setup
814813
815-
export const createStore(rootReducer)
814+
export const store = createStore(rootReducer)
816815
817816
// ❌ Common pattern: an "action type union" of all possible actions
818817
export type RootAction = TodoActions | CounterActions

0 commit comments

Comments
 (0)