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
ngRedux lets you easily connect your angular components with Redux.
19
-
the API is straightforward:
20
-
21
-
```JS
22
-
$ngRedux.connect(selector, callback);
23
-
```
24
-
25
-
Where `selector` is a function that takes Redux's entire store state as argument and returns an object that contains the slices of store state that your component is interested in.
26
-
e.g:
27
-
```JS
28
-
state=> ({todos:state.todos})
29
-
```
30
-
Note: if you are not familiar with this syntax, go and check out the [MDN Guide on fat arrow functions (ES2015)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
12
+
*ngRedux lets you easily connect your angular components with Redux.*
31
13
32
-
If you haven't, check out [reselect](https://github.com/faassen/reselect), an awesome tool to create and combine selectors.
33
14
15
+
## Table of Contents
34
16
35
-
This returned object will be passed as argument to the callback provided whenever the state changes.
36
-
ngRedux checks for shallow equality of the state's selected slice whenever the Store is updated, and will call the callback only if there is a change.
37
-
**Important: It is assumed that you never mutate your states, if you do mutate them, ng-redux will not execute the callback properly.**
38
-
See [Redux's doc](http://gaearon.github.io/redux/docs/basics/Reducers.html) to understand why you should not mutate your states.
* reducer (Function): A single reducer composed of all other reducers (create with redux.combineReducer)
50
-
*[middleware] (Array of Function or String): An array containing all the middleware that should be applied. Functions and strings are both valid members. String will be resolved via Angular, allowing you to use dependency injection in your middlewares.
51
-
* storeEnhancer: Optional function that will be used to create the store, in most cases you don't need that, see [Store Enhancer official doc](http://rackt.github.io/redux/docs/Glossary.html#store-enhancer)
Creates the Redux store, and allow `connect()` to access it.
92
66
93
-
You can also grab multiple slices of the state by passing an array of selectors:
67
+
#### Arguments:
68
+
*[`reducer`]\(*Function*): A single reducer composed of all other reducers (create with redux.combineReducer)
69
+
*[`middlewares`]\(*Function[]*): Optional, An array containing all the middleware that should be applied. Functions and strings are both valid members. String will be resolved via Angular, allowing you to use dependency injection in your middlewares.
70
+
*[`storeEnhancers`]\(*Function[]*): Optional, this will be used to create the store, in most cases you don't need to pass anything, see [Store Enhancer official documentation.](http://rackt.github.io/redux/docs/Glossary.html#store-enhancer)
94
71
95
-
```JS
96
-
constructor($ngRedux) {
97
-
this.todos= [];
98
-
this.users= [];
99
-
$ngRedux.connect(state=> ({
100
-
todos:state.todos,
101
-
users:state.users
102
-
}),
103
-
({todos, users}) => {
104
-
this.todos= todos
105
-
this.users= users;
106
-
});
107
-
}
108
-
```
109
72
73
+
### `connect([mapStateToTarget], [target])`
110
74
111
-
#### Unsubscribing
75
+
Connects an Angular component to Redux.
112
76
113
-
You can close a connection like this:
77
+
#### Arguments
78
+
*[`mapStateToTarget`]\(*Function*): connect will subscribe to Redux store updates. Any time it updates, mapStateToTarget will be called. Its result must be a plain object, and it will be merged into `target`.
79
+
*[`target`]\(*Object*): A plain object, this will be use as a target by `mapStateToTarget`. Read the Remarks below about the implication of passing `$scope` to `connect`.
114
80
115
-
```JS
81
+
#### Returns
82
+
(*Function*): A function that unsubscribes the change listener.
If `$scope` is passed to `connect` as `target`, ngRedux will listen to the `$destroy` event and unsubscribe the change listener when it is triggered, you don't need to keep track of your subscribtions in this case.
86
+
If anything else than `$scope` is passed as target, the responsability to unsubscribe correctly is deferred to the user.
121
87
122
-
destroy() {
123
-
this.unsubscribe();
124
-
}
88
+
### Store API
89
+
All of redux's store methods (i.e. `dispatch`, `subscribe` and `getState`) are exposed by $ngRedux and can be accessed directly. For example:
All of redux's store methods (i.e. `dispatch`, `subscribe` and `getState`) are exposed by $ngRedux and can be accessed directly. For example:
96
+
##Using DevTools
97
+
In order to use Redux DevTools with your angular app, you need to install [react](https://www.npmjs.com/package/react), [react-redux](https://www.npmjs.com/package/react-redux) and [redux-devtools](https://www.npmjs.com/package/redux-devtools) as development dependencies.
0 commit comments