2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Use the Spread Operator on Arrays
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Use the Spread Operator on Arrays
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-03-05 02:38:40 +03:00
|
|
|
```javascript
|
2019-07-24 00:59:27 -07:00
|
|
|
const immutableReducer = (state = ["Do not mutate state!"], action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case "ADD_TO_DO":
|
2019-03-05 02:38:40 +03:00
|
|
|
// don't mutate state here or the tests will fail
|
2019-05-05 12:40:32 -05:00
|
|
|
let arr = [...state, action.todo];
|
2019-03-05 02:38:40 +03:00
|
|
|
return arr;
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
const addToDo = todo => {
|
2019-03-05 02:38:40 +03:00
|
|
|
return {
|
2019-07-24 00:59:27 -07:00
|
|
|
type: "ADD_TO_DO",
|
2019-03-05 02:38:40 +03:00
|
|
|
todo
|
2019-07-24 00:59:27 -07:00
|
|
|
};
|
|
|
|
};
|
2019-03-05 02:38:40 +03:00
|
|
|
|
|
|
|
const store = Redux.createStore(immutableReducer);
|
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|