Skip to content

docs: replace slice calls to map #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions docs/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ function todos(state = [], action) {
]
case 'COMPLETE_TODO':
// Return a new array
return [
...state.slice(0, action.index),
// Copy the object before mutating
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
return state.map((todo, index) => {
if(index === action.index) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we leave space after if pretty much everywhere else.
Would you mind fixing this as a separate PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, attached it to #1478

// Copy the object before mutating
return Object.assign({}, todo, {
completed: true
})
}
return todo
})
default:
return state
}
Expand All @@ -71,13 +72,14 @@ It’s more code, but it’s exactly what makes Redux predictable and efficient.

```js
// Before:
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
return state.map((todo, index) => {
if(index === action.index) {
return Object.assign({}, todo, {
completed: true
})
}
return todo
})

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

```js
// Before:
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
return state.map((todo, index) => {
if(index === action.index) {
return Object.assign({}, todo, {
completed: true
})
}
return todo
})

// After:
return [
...state.slice(0, action.index),
{ ...state[action.index], completed: true },
...state.slice(action.index + 1)
]
return state.map((todo, index) => {
if(index === action.index) {
return { ...todo, completed: true }
}
return todo
})
```

Note that experimental language features are subject to change.
Expand Down
75 changes: 40 additions & 35 deletions docs/basics/Reducers.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ Finally, the implementation of the `COMPLETE_TODO` handler shouldn’t come as a
```js
case COMPLETE_TODO:
return Object.assign({}, state, {
todos: [
...state.todos.slice(0, action.index),
Object.assign({}, state.todos[action.index], {
completed: true
}),
...state.todos.slice(action.index + 1)
]
todos: state.todos.map((todo, index) => {
if(index === action.index) {
return Object.assign({}, todo, {
completed: true
})
}
return todo
})
})
```

Expand Down Expand Up @@ -182,13 +183,14 @@ function todoApp(state = initialState, action) {
})
case COMPLETE_TODO:
return Object.assign({}, state, {
todos: [
...state.todos.slice(0, action.index),
Object.assign({}, state.todos[action.index], {
completed: true
}),
...state.todos.slice(action.index + 1)
]
todos: state.todos.map((todo, index) => {
if(index === action.index) {
return Object.assign({}, todo, {
completed: true
})
}
return todo
})
})
default:
return state
Expand All @@ -210,13 +212,14 @@ function todos(state = [], action) {
}
]
case COMPLETE_TODO:
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
return state.map(todo, index) => {
if(index === action.index) {
return Object.assign({}, todo, {
completed: true
})
}
return todo
}
default:
return state
}
Expand Down Expand Up @@ -268,13 +271,14 @@ function todos(state = [], action) {
}
]
case COMPLETE_TODO:
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
return state.map((todo, index) => {
if(index === action.index) {
return Object.assign({}, todo, {
completed: true
})
}
return todo
})
default:
return state
}
Expand Down Expand Up @@ -389,13 +393,14 @@ function todos(state = [], action) {
}
]
case COMPLETE_TODO:
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
return state.map((todo, index) => {
if(index === action.index) {
return Object.assign({}, todo, {
completed: true
})
}
return todo
})
default:
return state
}
Expand Down
17 changes: 9 additions & 8 deletions docs/introduction/ThreePrinciples.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ console.log(store.getState())
{
text: 'Consider using Redux',
completed: true,
},
},
{
text: 'Keep all state in a single tree',
completed: false
Expand Down Expand Up @@ -74,13 +74,14 @@ function todos(state = [], action) {
}
]
case 'COMPLETE_TODO':
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
return state.map((todo, index) => {
if(index === action.index) {
return Object.assign({}, todo, {
completed: true
})
}
return todo
})
default:
return state
}
Expand Down