Skip to content

Commit d2d0824

Browse files
committed
Merge pull request reduxjs#1477 from just-boris/master
docs: replace slice calls to map
2 parents 32e282b + 4f06bfd commit d2d0824

File tree

3 files changed

+80
-70
lines changed

3 files changed

+80
-70
lines changed

docs/Troubleshooting.md

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ function todos(state = [], action) {
5353
]
5454
case 'COMPLETE_TODO':
5555
// Return a new array
56-
return [
57-
...state.slice(0, action.index),
58-
// Copy the object before mutating
59-
Object.assign({}, state[action.index], {
60-
completed: true
61-
}),
62-
...state.slice(action.index + 1)
63-
]
56+
return state.map((todo, index) => {
57+
if(index === action.index) {
58+
// Copy the object before mutating
59+
return Object.assign({}, todo, {
60+
completed: true
61+
})
62+
}
63+
return todo
64+
})
6465
default:
6566
return state
6667
}
@@ -71,13 +72,14 @@ It’s more code, but it’s exactly what makes Redux predictable and efficient.
7172

7273
```js
7374
// Before:
74-
return [
75-
...state.slice(0, action.index),
76-
Object.assign({}, state[action.index], {
77-
completed: true
78-
}),
79-
...state.slice(action.index + 1)
80-
]
75+
return state.map((todo, index) => {
76+
if(index === action.index) {
77+
return Object.assign({}, todo, {
78+
completed: true
79+
})
80+
}
81+
return todo
82+
})
8183

8284
// After
8385
return update(state, {
@@ -97,20 +99,22 @@ You can also enable the [object spread operator proposal](recipes/UsingObjectSpr
9799

98100
```js
99101
// Before:
100-
return [
101-
...state.slice(0, action.index),
102-
Object.assign({}, state[action.index], {
103-
completed: true
104-
}),
105-
...state.slice(action.index + 1)
106-
]
102+
return state.map((todo, index) => {
103+
if(index === action.index) {
104+
return Object.assign({}, todo, {
105+
completed: true
106+
})
107+
}
108+
return todo
109+
})
107110

108111
// After:
109-
return [
110-
...state.slice(0, action.index),
111-
{ ...state[action.index], completed: true },
112-
...state.slice(action.index + 1)
113-
]
112+
return state.map((todo, index) => {
113+
if(index === action.index) {
114+
return { ...todo, completed: true }
115+
}
116+
return todo
117+
})
114118
```
115119

116120
Note that experimental language features are subject to change.

docs/basics/Reducers.md

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,14 @@ Finally, the implementation of the `COMPLETE_TODO` handler shouldn’t come as a
147147
```js
148148
case COMPLETE_TODO:
149149
return Object.assign({}, state, {
150-
todos: [
151-
...state.todos.slice(0, action.index),
152-
Object.assign({}, state.todos[action.index], {
153-
completed: true
154-
}),
155-
...state.todos.slice(action.index + 1)
156-
]
150+
todos: state.todos.map((todo, index) => {
151+
if(index === action.index) {
152+
return Object.assign({}, todo, {
153+
completed: true
154+
})
155+
}
156+
return todo
157+
})
157158
})
158159
```
159160

@@ -182,13 +183,14 @@ function todoApp(state = initialState, action) {
182183
})
183184
case COMPLETE_TODO:
184185
return Object.assign({}, state, {
185-
todos: [
186-
...state.todos.slice(0, action.index),
187-
Object.assign({}, state.todos[action.index], {
188-
completed: true
189-
}),
190-
...state.todos.slice(action.index + 1)
191-
]
186+
todos: state.todos.map((todo, index) => {
187+
if(index === action.index) {
188+
return Object.assign({}, todo, {
189+
completed: true
190+
})
191+
}
192+
return todo
193+
})
192194
})
193195
default:
194196
return state
@@ -210,13 +212,14 @@ function todos(state = [], action) {
210212
}
211213
]
212214
case COMPLETE_TODO:
213-
return [
214-
...state.slice(0, action.index),
215-
Object.assign({}, state[action.index], {
216-
completed: true
217-
}),
218-
...state.slice(action.index + 1)
219-
]
215+
return state.map(todo, index) => {
216+
if(index === action.index) {
217+
return Object.assign({}, todo, {
218+
completed: true
219+
})
220+
}
221+
return todo
222+
}
220223
default:
221224
return state
222225
}
@@ -268,13 +271,14 @@ function todos(state = [], action) {
268271
}
269272
]
270273
case COMPLETE_TODO:
271-
return [
272-
...state.slice(0, action.index),
273-
Object.assign({}, state[action.index], {
274-
completed: true
275-
}),
276-
...state.slice(action.index + 1)
277-
]
274+
return state.map((todo, index) => {
275+
if(index === action.index) {
276+
return Object.assign({}, todo, {
277+
completed: true
278+
})
279+
}
280+
return todo
281+
})
278282
default:
279283
return state
280284
}
@@ -389,13 +393,14 @@ function todos(state = [], action) {
389393
}
390394
]
391395
case COMPLETE_TODO:
392-
return [
393-
...state.slice(0, action.index),
394-
Object.assign({}, state[action.index], {
395-
completed: true
396-
}),
397-
...state.slice(action.index + 1)
398-
]
396+
return state.map((todo, index) => {
397+
if(index === action.index) {
398+
return Object.assign({}, todo, {
399+
completed: true
400+
})
401+
}
402+
return todo
403+
})
399404
default:
400405
return state
401406
}

docs/introduction/ThreePrinciples.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ console.log(store.getState())
1818
{
1919
text: 'Consider using Redux',
2020
completed: true,
21-
},
21+
},
2222
{
2323
text: 'Keep all state in a single tree',
2424
completed: false
@@ -74,13 +74,14 @@ function todos(state = [], action) {
7474
}
7575
]
7676
case 'COMPLETE_TODO':
77-
return [
78-
...state.slice(0, action.index),
79-
Object.assign({}, state[action.index], {
80-
completed: true
81-
}),
82-
...state.slice(action.index + 1)
83-
]
77+
return state.map((todo, index) => {
78+
if(index === action.index) {
79+
return Object.assign({}, todo, {
80+
completed: true
81+
})
82+
}
83+
return todo
84+
})
8485
default:
8586
return state
8687
}

0 commit comments

Comments
 (0)