@@ -40,9 +40,9 @@ We can do this by hand using JavaScript's array / object spread operators, as we
40
40
const obj = {
41
41
a: {
42
42
// To safely update obj.a.c, we have to copy each piece
43
- c: 3
43
+ c: 3 ,
44
44
},
45
- b: 2
45
+ b: 2 ,
46
46
}
47
47
48
48
const obj2 = {
@@ -53,8 +53,8 @@ const obj2 = {
53
53
// copy obj.a
54
54
... obj .a ,
55
55
// overwrite c
56
- c: 42
57
- }
56
+ c: 42 ,
57
+ },
58
58
}
59
59
60
60
const arr = [' a' , ' b' ]
@@ -107,7 +107,7 @@ So if we can't change the originals, how do we return an updated state?
107
107
// ✅ This is safe, because we made a copy
108
108
return {
109
109
... state,
110
- value: 123
110
+ value: 123 ,
111
111
}
112
112
```
113
113
@@ -129,10 +129,10 @@ function handwrittenReducer(state, action) {
129
129
... state .first .second ,
130
130
[action .someId ]: {
131
131
... state .first .second [action .someId ],
132
- fourth: action .someValue
133
- }
134
- }
135
- }
132
+ fourth: action .someValue ,
133
+ },
134
+ },
135
+ },
136
136
}
137
137
}
138
138
```
@@ -153,15 +153,15 @@ import produce from 'immer'
153
153
const baseState = [
154
154
{
155
155
todo: ' Learn typescript' ,
156
- done: true
156
+ done: true ,
157
157
},
158
158
{
159
159
todo: ' Try immer' ,
160
- done: false
161
- }
160
+ done: false ,
161
+ },
162
162
]
163
163
164
- const nextState = produce (baseState, draftState => {
164
+ const nextState = produce (baseState, ( draftState ) => {
165
165
// "mutate" the draft array
166
166
draftState .push ({ todo: ' Tweet about it' })
167
167
// "mutate" the nested state
@@ -181,7 +181,7 @@ console.log(baseState[1] === nextState[1])
181
181
Redux Toolkit's [ ` createReducer ` API] ( ../api/createReducer.mdx ) uses Immer internally automatically. So, it's already safe to "mutate" state inside of any case reducer function that is passed to ` createReducer ` :
182
182
183
183
``` js
184
- const todosReducer = createReducer ([], builder => {
184
+ const todosReducer = createReducer ([], ( builder ) => {
185
185
builder .addCase (' todos/todoAdded' , (state , action ) => {
186
186
// "mutate" the array by calling push()
187
187
state .push (action .payload )
@@ -198,8 +198,8 @@ const todosSlice = createSlice({
198
198
reducers: {
199
199
todoAdded (state , action ) {
200
200
state .push (action .payload )
201
- }
202
- }
201
+ },
202
+ },
203
203
})
204
204
```
205
205
@@ -214,8 +214,8 @@ const todosSlice = createSlice({
214
214
name: ' todos' ,
215
215
initialState: [],
216
216
reducers: {
217
- todoAdded: addItemToArray
218
- }
217
+ todoAdded: addItemToArray,
218
+ },
219
219
})
220
220
```
221
221
@@ -285,8 +285,8 @@ const todosSlice = createSlice({
285
285
// ✅ SAFE: curly braces make this a function body and no return
286
286
fixedReducer2 : (state , action ) => {
287
287
state .push (action .payload )
288
- }
289
- }
288
+ },
289
+ },
290
290
})
291
291
```
292
292
@@ -300,7 +300,7 @@ function objectCaseReducer1(state, action) {
300
300
a,
301
301
b,
302
302
c,
303
- d
303
+ d,
304
304
}
305
305
}
306
306
@@ -352,8 +352,8 @@ const todosSlice = createSlice({
352
352
correctResetTodosReducer (state , action ) {
353
353
// ✅ CORRECT: returns a new value to replace the old one
354
354
return initialState
355
- }
356
- }
355
+ },
356
+ },
357
357
})
358
358
```
359
359
@@ -377,8 +377,8 @@ const todosSlice = createSlice({
377
377
console .log (state)
378
378
// ✅ CORRECT: logs a plain JS copy of the current data
379
379
console .log (current (state))
380
- }
381
- }
380
+ },
381
+ },
382
382
})
383
383
```
384
384
@@ -400,21 +400,21 @@ const todosSlice = createSlice({
400
400
initialState: [],
401
401
reducers: {
402
402
brokenTodoToggled (state , action ) {
403
- const todo = state .find (todo => todo .id === action .payload )
403
+ const todo = state .find (( todo ) => todo .id === action .payload )
404
404
if (todo) {
405
405
// ❌ ERROR: Immer can't track updates to a primitive value!
406
406
let { completed } = todo
407
407
completed = ! completed
408
408
}
409
409
},
410
410
fixedTodoToggled (state , action ) {
411
- const todo = state .find (todo => todo .id === action .payload )
411
+ const todo = state .find (( todo ) => todo .id === action .payload )
412
412
if (todo) {
413
413
// ✅ CORRECT: This object is still wrapped in a Proxy, so we can "mutate" it
414
414
todo .completed = ! todo .completed
415
415
}
416
- }
417
- }
416
+ },
417
+ },
418
418
})
419
419
```
420
420
@@ -442,8 +442,8 @@ const itemsSlice = createSlice({
442
442
}
443
443
444
444
state[id].push (item)
445
- }
446
- }
445
+ },
446
+ },
447
447
})
448
448
```
449
449
0 commit comments