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
By passing it to `connect`, our component receives it as a prop, and it will automatically dispatch the action when it’s called.
205
205
206
-
```jsx
206
+
```js
207
207
// components/AddTodo.js
208
208
209
209
// ... other imports
@@ -277,7 +277,7 @@ The `<TodoList />` component is responsible for rendering the list of todos. The
277
277
278
278
Our `<Todo />` component takes the todo item as props. We have this information from the `byIds` field of the `todos`. However, we also need the information from the `allIds` field of the store indicating which todos and in what order they should be rendered. Our `mapStateToProps` function may look like this:
@@ -354,7 +354,7 @@ If you call `connect` without providing any arguments, your component will:
354
354
-_not_ re-render when the store changes
355
355
- receive `props.dispatch` that you may use to manually dispatch action
356
356
357
-
```jsx
357
+
```js
358
358
// ... Component
359
359
exportdefaultconnect()(Component); // Component will receive `dispatch` (just like our <TodoList />!)
360
360
```
@@ -366,7 +366,7 @@ If you call `connect` with only `mapStateToProps`, your component will:
366
366
- subscribe to the values that `mapStateToProps` extracts from the store, and re-render only when those values have changed
367
367
- receive `props.dispatch` that you may use to manually dispatch action
368
368
369
-
```jsx
369
+
```js
370
370
// ... Component
371
371
constmapStateToProps=state=>state.partOfState;
372
372
exportdefaultconnect(mapStateToProps)(Component);
@@ -379,7 +379,7 @@ If you call `connect` with only `mapDispatchToProps`, your component will:
379
379
-_not_ re-render when the store changes
380
380
- receive each of the action creators you inject with `mapDispatchToProps` as props and automatically dispatch the actions upon being called
381
381
382
-
```jsx
382
+
```js
383
383
import { addTodo } from"./actionCreators";
384
384
// ... Component
385
385
exportdefaultconnect(
@@ -395,7 +395,7 @@ If you call `connect` with both `mapStateToProps` and `mapDispatchToProps`, your
395
395
- subscribe to the values that `mapStateToProps` extracts from the store, and re-render only when those values have changed
396
396
- receive all of the action creators you inject with `mapDispatchToProps` as props and automatically dispatch the actions upon being called.
397
397
398
-
```jsx
398
+
```js
399
399
import*asactionCreatorsfrom"./actionCreators";
400
400
// ... Component
401
401
constmapStateToProps=state=>state.partOfState;
@@ -415,7 +415,7 @@ Now let’s connect the rest of our `<TodoApp />`.
415
415
416
416
How should we implement the interaction of toggling todos? A keen reader might already have an answer. If you have your environment set up and have followed through up until this point, now is a good time to leave it aside and implement the feature by yourself. There would be no surprise that we connect our `<Todo />` to dispatch `toggleTodo` in a similar way:
The `<VisibilityFilters />` component needs to be able to read from the store which filter is currently active, and dispatch actions to the store. Therefore, we need to pass both a `mapStateToProps` and `mapDispatchToProps`. The `mapStateToProps` here can be a simple accessor of the `visibilityFilter` state. And the `mapDispatchToProps` will contain the `setFilter` action creator.
Copy file name to clipboardExpand all lines: docs/api.md
+11-9
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ If you *really* need to, you can manually pass `store` as a prop to every `conne
23
23
24
24
#### Vanilla React
25
25
26
-
```js
26
+
```jsx
27
27
ReactDOM.render(
28
28
<Provider store={store}>
29
29
<MyRootComponent />
@@ -34,7 +34,7 @@ ReactDOM.render(
34
34
35
35
#### React Router
36
36
37
-
```js
37
+
```jsx
38
38
ReactDOM.render(
39
39
<Provider store={store}>
40
40
<Router history={history}>
@@ -92,32 +92,33 @@ It does not modify the component class passed to it; instead, it *returns* a new
92
92
#### The arity of mapStateToProps and mapDispatchToProps determines whether they receive ownProps
93
93
94
94
> Note: `ownProps`**is not passed** to `mapStateToProps` and `mapDispatchToProps` if the formal definition of the function contains one mandatory parameter (function has length 1). For example, functions defined like below won't receive `ownProps` as the second argument.
95
-
```javascript
95
+
96
+
```js
96
97
functionmapStateToProps(state) {
97
98
console.log(state); // state
98
99
console.log(arguments[1]); // undefined
99
100
}
100
101
```
101
-
```javascript
102
+
```js
102
103
constmapStateToProps= (state, ownProps= {}) => {
103
104
console.log(state); // state
104
105
console.log(ownProps); // undefined
105
106
}
106
107
```
107
108
Functions with no mandatory parameters or two parameters **will receive**`ownProps`.
Copy file name to clipboardExpand all lines: docs/troubleshooting.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ If you’re using React Router 0.13, you might [bump into this problem](https://
28
28
29
29
Root view:
30
30
31
-
```js
31
+
```jsx
32
32
Router.run(routes, Router.HistoryLocation, (Handler, routerState) => { // note "routerState" here
33
33
ReactDOM.render(
34
34
<Provider store={store}>
@@ -62,7 +62,7 @@ The _best_ solution to this is to make sure that your components are pure and pa
62
62
63
63
If that’s not practical for whatever reason (for example, if you’re using a library that depends heavily on React context), you may pass the `pure: false` option to `connect()`:
0 commit comments