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/tutorials/fundamentals/part-1-overview.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -131,7 +131,7 @@ The center of every Redux application is the **store**. A "store" is a container
131
131
132
132
A store is a JavaScript object with a few special functions and abilities that make it different than a plain global object:
133
133
134
-
- You cannot directly modify or change the state that is kept inside the Redux store
134
+
- You must never directly modify or change the state that is kept inside the Redux store
135
135
- Instead, the only way to cause an update to the state is to create a plain **action** object that describes "something that happened in the application", and then **dispatch** the action to the store to tell it what happened.
136
136
- When an action is dispatched, the store runs the root **reducer** function, and lets it calculate the new state based on the old state and the action
137
137
- Finally, the store notifies **subscribers** that the state has been updated so the UI can be updated with the new data.
@@ -307,7 +307,7 @@ That counter example was small, but it does show all the working pieces of a rea
307
307
308
308
With that in mind, let's review what we've learned so far:
309
309
310
-
:::tip
310
+
:::Summary
311
311
312
312
-**Redux is a library for managing global application state**
313
313
- Redux is typically used with the React-Redux library for integrating Redux and React together
Copy file name to clipboardExpand all lines: docs/tutorials/fundamentals/part-4-store.md
+23-2
Original file line number
Diff line number
Diff line change
@@ -156,7 +156,7 @@ Notice that our app did _not_ log anything from the last action. That's because
156
156
We specified the behavior of our app before we even started writing the UI. That
157
157
helps give us confidence that the app will work as intended.
158
158
159
-
:::tip
159
+
:::info
160
160
161
161
If you want, you can now try writing tests for your reducers. Because they're [pure functions](../../understanding/thinking-in-redux/ThreePrinciples.md#changes-are-made-with-pure-functions), it should be straightforward to test them. Call them with an example `state` and `action`,
162
162
take the result, and check to see if it matches what you expect:
@@ -218,6 +218,27 @@ As you can see, the actual logic here is fairly short:
218
218
- The store dispatches one action on startup to initialize the reducers with their state
219
219
- The store API is an object with `{dispatch, subscribe, getState}` inside
220
220
221
+
To emphasize one of those in particular: notice that `getState` just returns whatever the current `state` value is. That means that **by default, nothing prevents you from accidentally mutating the current state value!** This code will run without any errors, but it's incorrect:
222
+
223
+
```js
224
+
conststate=store.getState()
225
+
// ❌ Don't do this - it mutates the current state!
226
+
state.filters.status='Active'
227
+
```
228
+
229
+
In other words:
230
+
231
+
- The Redux store doesn't make an extra copy of the `state` value when you call `getState()`. It's exactly the same reference that was returned from the root reducer function
232
+
- The Redux store doesn't do anything else to prevent accidental mutations. It _is_ possible to mutate the state, either inside a reducer or outside the store, and you must always be careful to avoid mutations.
233
+
234
+
One common cause of accidental mutations is sorting arrays. [**Calling `array.sort()` actually mutates the existing array**](https://doesitmutate.xyz/sort/). If we called `const sortedTodos = state.todos.sort()`, we'd end up mutating the real store state unintentionally.
235
+
236
+
:::tip
237
+
238
+
In [Part 8: Modern Redux](./part-8-modern-redux.md), we'll see how Redux Toolkit helps avoid mutations in reducers, and detects and warns about accidental mutations outside of reducers.
239
+
240
+
:::
241
+
221
242
## Configuring the Store
222
243
223
244
We've already seen that we can pass `rootReducer` and `preloadedState` arguments to `createStore`. However, `createStore` can also take one more argument, which is used to customize the store's abilities and give it new powers.
@@ -585,7 +606,7 @@ Let's see how our example app looks now:
585
606
586
607
And as a reminder, here's what we covered in this section:
587
608
588
-
:::tip
609
+
:::Summary
589
610
590
611
-**Redux apps always have a single store**
591
612
- Stores are created with the Redux `createStore` API
Copy file name to clipboardExpand all lines: docs/tutorials/fundamentals/part-5-ui-and-react.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ In this section, we'll add a User Interface for our todo app. We'll see how Redu
25
25
26
26
## Integrating Redux with a UI
27
27
28
-
Redux is a standalone JS library. As we've already seen, you can create and use a Redux store even if you don't have a user interface set up. This also means that **you can use Redux with any UI framework**. You can write Redux apps with React, Vue, Angular, Ember, jQuery, or vanilla JavaScript.
28
+
Redux is a standalone JS library. As we've already seen, you can create and use a Redux store even if you don't have a user interface set up. This also means that **you can use Redux with any UI framework** (or even without _any_ UI framework), and use it on both client and server. You can write Redux apps with React, Vue, Angular, Ember, jQuery, or vanilla JavaScript.
29
29
30
30
That said, **Redux was specifically designed to work well with [React](https://reactjs.org)**. React lets you describe your UI as a function of your state, and Redux contains state and updates it in response to actions.
31
31
@@ -553,7 +553,7 @@ Let's see how the app looks now, including the components and sections we skippe
0 commit comments