Skip to content

Commit 9492f24

Browse files
committed
Move object spread docs around
1 parent 0a8e046 commit 9492f24

File tree

8 files changed

+24
-22
lines changed

8 files changed

+24
-22
lines changed

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Next Steps
2424
* [Recipes](/docs/recipes/README.md)
2525
* [Migrating to Redux](/docs/recipes/MigratingToRedux.md)
26+
* [Using Object Spread Operator](/docs/recipes/UsingObjectSpreadOperator.md)
2627
* [Reducing Boilerplate](/docs/recipes/ReducingBoilerplate.md)
2728
* [Server Rendering](/docs/recipes/ServerRendering.md)
2829
* [Writing Tests](/docs/recipes/WritingTests.md)

docs/Troubleshooting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Finally, to update objects, you’ll need something like `_.extend` from Undersc
9393

9494
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`.
9595

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:
9797

9898
```js
9999
// Before:
@@ -113,7 +113,7 @@ return [
113113
]
114114
```
115115

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.
117117

118118
#### Don’t forget to call [`dispatch(action)`](api/Store.md#dispatch)
119119

docs/advanced/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ In the [basics walkthrough](../basics/README.md), we explored how to structure a
66
* [Async Flow](AsyncFlow.md)
77
* [Middleware](Middleware.md)
88
* [Usage with React Router](UsageWithReactRouter.md)
9-
* [Object Spread Operator](ObjectSpreadOperator.md)
109
* [Example: Reddit API](ExampleRedditAPI.md)
1110
* [Next Steps](NextSteps.md)

docs/api/createStore.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ console.log(store.getState())
4646

4747
* 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.
4848

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).
5050

5151
* 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.
5252

docs/basics/Reducers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function todoApp(state = initialState, action) {
9999

100100
Note that:
101101

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.
103103

104104
2. **We return the previous `state` in the `default` case.** It’s important to return the previous `state` for any unknown action.
105105

docs/introduction/PriorArt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Redux was inspired by several important qualities of Flux. Like Flux, Redux pres
1313

1414
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.
1515

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).
1717

1818
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.
1919

docs/recipes/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
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.
44

55
* [Migrating to Redux](MigratingToRedux.md)
6+
* [Using Object Spread Operator](UsingObjectSpreadOperator.md)
67
* [Reducing Boilerplate](ReducingBoilerplate.md)
78
* [Server Rendering](ServerRendering.md)
89
* [Writing Tests](WritingTests.md)

docs/advanced/ObjectSpreadOperator.md renamed to docs/recipes/UsingObjectSpreadOperator.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Object Spread Operator
1+
# Using Object Spread Operator
22

33
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
44
copies of objects with new or updated values. For example, in the `todoApp` below `Object.assign()` is used to return a new
55
`state` object with an updated `visibilityFilter` property:
66

77
```js
8-
function todoApp(state = initialState, action) {
8+
function todoApp(state = initialState, action) {
99
switch (action.type) {
1010
case SET_VISIBILITY_FILTER:
1111
return Object.assign({}, state, {
@@ -36,24 +36,23 @@ function todoApp(state = initialState, action) {
3636
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`.
3737

3838
```js
39-
return getAddedIds(state.cart).map(id => Object.assign(
40-
{},
41-
getProduct(state.products, id),
42-
{
43-
quantity: getQuantity(state.cart, id)
44-
}
45-
))
46-
```
47-
39+
return getAddedIds(state.cart).map(id => Object.assign(
40+
{},
41+
getProduct(state.products, id),
42+
{
43+
quantity: getQuantity(state.cart, id)
44+
}
45+
))
46+
```
4847

4948
Object spread lets us simplify the above `map` call to:
5049

5150
```js
52-
return getAddedIds(state.cart).map(id => ({
53-
...getProduct(state.products, id),
54-
quantity: getQuantity(state.cart, id)
55-
}))
56-
```
51+
return getAddedIds(state.cart).map(id => ({
52+
...getProduct(state.products, id),
53+
quantity: getQuantity(state.cart, id)
54+
}))
55+
```
5756

5857
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`.
5958

@@ -63,3 +62,5 @@ Since the object spread syntax is still a Stage 2 proposal for ECMAScript you’
6362
"plugins": ["transform-object-rest-spread"]
6463
}
6564
```
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

Comments
 (0)