You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Troubleshooting.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,7 +93,7 @@ Finally, to update objects, you’ll need something like `_.extend` from Undersc
93
93
94
94
Make sure that you use `Object.assign` correctly. For example, instead of returning something like `Object.assign(state, newData)` from your reducers, return `Object.assign({}, state, newData)`. This way you don’t override the previous `state`.
95
95
96
-
You can also enable [ES7 object spread proposal](https://github.com/sebmarkbage/ecmascript-rest-spread) with [Babel transform-object-rest-spread plugin](http://babeljs.io/docs/plugins/transform-object-rest-spread/):
96
+
You can also enable the [object spread operator proposal](recipes/UsingObjectSpreadOperator.md) for a more succinct syntax:
97
97
98
98
```js
99
99
// Before:
@@ -113,7 +113,7 @@ return [
113
113
]
114
114
```
115
115
116
-
Note that experimental language features are subject to change, and it’s unwise to rely on them in large codebases.
116
+
Note that experimental language features are subject to change.
117
117
118
118
#### Don’t forget to call [`dispatch(action)`](api/Store.md#dispatch)
Copy file name to clipboardExpand all lines: docs/api/createStore.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ console.log(store.getState())
46
46
47
47
* It is up to you to choose the state format. You can use plain objects or something like [Immutable](http://facebook.github.io/immutable-js/). If you’re not sure, start with plain objects.
48
48
49
-
* If your state is a plain object, make sure you never mutate it! For example, instead of returning something like `Object.assign(state, newData)` from your reducers, return `Object.assign({}, state, newData)`. This way you don’t override the previous `state`. You can also write `return { ...state, ...newData }` if you enable [ES7 object spread proposal](https://github.com/sebmarkbage/ecmascript-rest-spread) with [Babel transform-object-rest-spread plugin](http://babeljs.io/docs/plugins/transform-object-rest-spread/).
49
+
* If your state is a plain object, make sure you never mutate it! For example, instead of returning something like `Object.assign(state, newData)` from your reducers, return `Object.assign({}, state, newData)`. This way you don’t override the previous `state`. You can also write `return { ...state, ...newData }` if you enable the [object spread operator proposal](../recipes/UsingObjectSpreadOperator.md).
50
50
51
51
* For universal apps that run on the server, create a store instance with every request so that they are isolated. Dispatch a few data fetching actions to a store instance and wait for them to complete before rendering the app on the server.
Copy file name to clipboardExpand all lines: docs/basics/Reducers.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,7 +99,7 @@ function todoApp(state = initialState, action) {
99
99
100
100
Note that:
101
101
102
-
1.**We don’t mutate the `state`.** We create a copy with [`Object.assign()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign). `Object.assign(state, { visibilityFilter: action.filter })` is also wrong: it will mutate the first argument. You **must** supply an empty object as the first parameter. You can also enable the experimental [object spread syntax](https://github.com/sebmarkbage/ecmascript-rest-spread) proposed for ES7 to write `{ ...state, ...newState }` instead.
102
+
1.**We don’t mutate the `state`.** We create a copy with [`Object.assign()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign). `Object.assign(state, { visibilityFilter: action.filter })` is also wrong: it will mutate the first argument. You **must** supply an empty object as the first parameter. You can also enable the [object spread operator proposal](../recipes/UsingObjectSpreadOperator.md) to write `{ ...state, ...newState }` instead.
103
103
104
104
2.**We return the previous `state` in the `default` case.** It’s important to return the previous `state` for any unknown action.
Copy file name to clipboardExpand all lines: docs/introduction/PriorArt.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Redux was inspired by several important qualities of Flux. Like Flux, Redux pres
13
13
14
14
Unlike Flux, **Redux does not have the concept of a Dispatcher**. This is because it relies on pure functions instead of event emitters, and pure functions are easy to compose and don’t need an additional entity managing them. Depending on how you view Flux, you may see this as either a deviation or an implementation detail. Flux has often been [described as `(state, action) => state`](https://speakerdeck.com/jmorrell/jsconf-uy-flux-those-who-forget-the-past-dot-dot-dot-1). In this sense, Redux is true to the Flux architecture, but makes it simpler thanks to pure functions.
15
15
16
-
Another important difference from Flux is that **Redux assumes you never mutate your data**. You can use plain objects and arrays for your state just fine, but mutating them inside the reducers is strongly discouraged. You should always return a new object, which is easy with the [object spread syntax proposed for ES7](https://github.com/sebmarkbage/ecmascript-rest-spread) and implemented in [Babel](http://babeljs.io), or with a library like [Immutable](https://facebook.github.io/immutable-js).
16
+
Another important difference from Flux is that **Redux assumes you never mutate your data**. You can use plain objects and arrays for your state just fine, but mutating them inside the reducers is strongly discouraged. You should always return a new object, which is easy with the [object spread operator proposal](../recipes/UsingObjectSpreadOperator.md), or with a library like [Immutable](https://facebook.github.io/immutable-js).
17
17
18
18
While it is technically *possible* to [write impure reducers](https://github.com/reactjs/redux/issues/328#issuecomment-125035516) that mutate the data for performance corner cases, we actively discourage you from doing this. Development features like time travel, record/replay, or hot reloading will break. Moreover it doesn’t seem like immutability poses performance problems in most real apps, because, as [Om](https://github.com/omcljs/om) demonstrates, even if you lose out on object allocation, you still win by avoiding expensive re-renders and re-calculations, as you know exactly what changed thanks to reducer purity.
Copy file name to clipboardExpand all lines: docs/recipes/README.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
These are some use cases and code snippets to get you started with Redux in a real app. They assume you understand the topics in [basic](../basics/README.md) and [advanced](../advanced/README.md) tutorials.
Copy file name to clipboardExpand all lines: docs/recipes/UsingObjectSpreadOperator.md
+17-16Lines changed: 17 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
-
# Object Spread Operator
1
+
# Using Object Spread Operator
2
2
3
3
Since one of the core tenants of Redux is to never mutate state, you’ll often find yourself using [`Object.assign()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) to create
4
4
copies of objects with new or updated values. For example, in the `todoApp` below `Object.assign()` is used to return a new
5
5
`state` object with an updated `visibilityFilter` property:
6
6
7
7
```js
8
-
functiontodoApp(state=initialState, action) {
8
+
functiontodoApp(state=initialState, action) {
9
9
switch (action.type) {
10
10
caseSET_VISIBILITY_FILTER:
11
11
returnObject.assign({}, state, {
@@ -36,24 +36,23 @@ function todoApp(state = initialState, action) {
36
36
The advantage of using the object spread syntax becomes more apparent when you’re composing complex objects. Below `getAddedIds` maps an array of `id` values to an array of objects with values returned from `getProduct` and `getQuantity`.
Object spread lets us simplify the above `map` call to:
50
49
51
50
```js
52
-
returngetAddedIds(state.cart).map(id=> ({
53
-
...getProduct(state.products, id),
54
-
quantity:getQuantity(state.cart, id)
55
-
}))
56
-
```
51
+
returngetAddedIds(state.cart).map(id=> ({
52
+
...getProduct(state.products, id),
53
+
quantity:getQuantity(state.cart, id)
54
+
}))
55
+
```
57
56
58
57
Since the object spread syntax is still a Stage 2 proposal for ECMAScript you’ll need to use a transpiler such as [Babel](http://babeljs.io/) to use it in production. You can use your existing `es2015` preset, install [`babel-plugin-transform-object-read-spread`](http://babeljs.io/docs/plugins/transform-object-rest-spread/) and add it individually to the `plugins` array in your `.babelrc`.
59
58
@@ -63,3 +62,5 @@ Since the object spread syntax is still a Stage 2 proposal for ECMAScript you’
63
62
"plugins": ["transform-object-rest-spread"]
64
63
}
65
64
```
65
+
66
+
Note that this is still an experimental language feature proposal so it may change in the future. Nevertheless some large projects such as [React Native](https://github.com/facebook/react-native) already use it extensively so it is safe to say that there will be a good automated migration path if it changes.
0 commit comments