Skip to content

Commit e17df18

Browse files
committed
Update README.md
1 parent bd6b7e8 commit e17df18

File tree

1 file changed

+99
-2
lines changed

1 file changed

+99
-2
lines changed

README.md

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,102 @@ Angular 2 bindings for Redux.
44

55
##### This is a work very much in progress, use at your own risk :)
66

7-
## How to use
8-
An example counter app can be found [here](https://github.com/wbuchwalter/SampleNg2redux)
7+
## Overview
8+
9+
ngRedux lets you easily connect your angular components with Redux.
10+
the API is straightforward:
11+
12+
```JS
13+
ngRedux.connect(selector, callback, disableCaching = false);
14+
//OR
15+
ngRedux.connect([selector1, selector2, ...], callback, disableCaching = false);
16+
```
17+
18+
Where selector is a function taking for single argument the entire redux Store's state (a plain JS object) and returns another object, which is the slice of the state that your component is interested in.
19+
e.g:
20+
```JS
21+
state => state.todos
22+
```
23+
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)
24+
25+
If you haven't, check out [reselect](https://github.com/faassen/reselect), an awesome tool to create and combine selectors.
26+
27+
28+
This returned object will be passed as argument to the callback provided whenever the state changes.
29+
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.
30+
##### Important: It is assumed that you never mutate your states, if you do mutate them, ng-redux will not execute the callback properly.
31+
See [Redux's doc](http://gaearon.github.io/redux/docs/basics/Reducers.html) to understand why you should not mutate your states.
32+
If you have a good reason to mutate your states, you can still [disable caching](#Disable-caching) altogether.
33+
34+
35+
## Getting Started
36+
37+
#### Initialization
38+
You need to pass Redux Store to ng-redux via ```$ngReduxProvider``` :
39+
40+
```JS
41+
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
42+
const store = createStoreWithMiddleware(rootReducer);
43+
44+
bootstrap(
45+
App,
46+
[bind('ngRedux').toFactory(() => {
47+
return new ngRedux(store);
48+
})]
49+
);
50+
```
51+
52+
#### Usage
53+
```JS
54+
@Component(...)
55+
@View(...)
56+
class SmartComponent {
57+
58+
constructor(@Inject('ngRedux') ngRedux) {
59+
this._ngRedux = ngRedux;
60+
ngRedux.connect(state => state.counter, counter => this.counter = counter);
61+
}
62+
63+
onInit() {
64+
this.actions = bindActionCreators(CounterActions, this._ngRedux.getStore().dispatch);
65+
}
66+
67+
onDestroy() {
68+
this._ngRedux.disconnect();
69+
}
70+
}
71+
```
72+
73+
##### Note: The callback provided to ```connect``` will be called once directly after creation to allow initialization of your component states
74+
75+
76+
77+
You can also grab multiple slices of the state by passing an array of selectors:
78+
79+
```JS
80+
ngRedux.connect([
81+
state => state.todos,
82+
state => state.users
83+
],
84+
(todos, users) => {
85+
this.todos = todos
86+
this.users = users;
87+
});
88+
```
89+
90+
91+
#### Accessing Redux' Store
92+
You don't need to create another service to get hold of Redux's store (although you can).
93+
You can access the store via ```ngRedux.getStore()```:
94+
95+
```JS
96+
redux.bindActionCreators(actionCreator, ngRedux.getStore().dispatch);
97+
```
98+
99+
#### Disabling caching
100+
Each time Redux's Store update, ng-redux will check if the slices specified via 'selectors' have changed, and if so will execute the provided callback.
101+
You can disable this behaviour, and force the callback to be executed even if the slices didn't change by setting ```disableCaching``` to true:
102+
103+
```JS
104+
reduxConnector.connect(state => state.todos, todos => this.todos = todos, true);
105+
```

0 commit comments

Comments
 (0)