|
6 | 6 | #Redux dynamics |
7 | 7 | Strongly-typed collection of useful methods and tools to make your[Redux](http://redux.js.org/) workflow more dynamic. |
8 | 8 |
|
| 9 | +##Features |
| 10 | +*`state` is always immutable |
| 11 | +*`action` is always immutable |
| 12 | +*`context` shared between all subscriptions |
| 13 | +* Declarative reducer subscriptions to the actions |
| 14 | +* Encouragement of pure resolver functions |
| 15 | +* Accept`RegExp` as subscribed action type |
| 16 | +***No huge`switch` statements anymore!** |
| 17 | + |
9 | 18 | ##Getting started |
10 | | -Install`redux-dynamics` using npm: |
11 | | -```basg |
| 19 | + |
| 20 | +###Install |
| 21 | + |
| 22 | +####NPM: |
| 23 | +```bash |
12 | 24 | npm install redux-dynamics --save |
13 | 25 | ``` |
14 | 26 |
|
15 | | -Include it in your project: |
| 27 | +####Yarn: |
| 28 | +```bash |
| 29 | +yarn add redux-dynamics |
| 30 | +``` |
| 31 | + |
| 32 | +###Create a reducer |
| 33 | + |
16 | 34 | ```js |
17 | | -/* ES5*/ |
18 | | -constreduxDynamics=require('redux-dynamics'); |
19 | | -constcreateReducer=require('redux-dynamics').createReducer; |
| 35 | +// store/comments/index.js |
| 36 | +import {Reducer }from'redux-dynamics'; |
20 | 37 |
|
21 | | -/* ES6+*/ |
22 | | -importreduxDynamicsfrom'redux-dynamics'; |
23 | | -import {createReducer }from'redux-dynamics'; |
| 38 | +/* Create a new reducer with initial state*/ |
| 39 | +constreducer=newReducer({ |
| 40 | + likes:0 |
| 41 | +}); |
| 42 | + |
| 43 | +/* Subscribe to action types*/ |
| 44 | +reducer.subscribe('ADD_LIKES', (state,action,context)=> { |
| 45 | +/* Note how both "state" and "action" are immutable*/ |
| 46 | +constnextLikes=state.get('likes')+action.get('amount'); |
| 47 | + |
| 48 | +/* Resolve the next state*/ |
| 49 | +returnstate.set('likes', nextLikes); |
| 50 | +}); |
24 | 51 | ``` |
25 | 52 |
|
26 | | -##Methods |
27 | | -###`createReducer({ initialState?: State, actions: Array<ExpectedAction> })` |
28 | | -####Features: |
29 | | -* Simplified declaration of initial state |
30 | | -* Enforced immutability of the`state` (using[Immutable](https://facebook.github.io/immutable-js)) |
31 | | -* Enforced immutability of the`action` for seamless integration with the state |
32 | | -* Scoped variables and logic for reducer functions |
33 | | -* Support of`RegExp` as an expected action type |
34 | | -* No explicit state return, it is always returned by default |
35 | | - |
36 | | -See[`createReducer` documentation](./docs/api/createReducer.md). |
| 53 | +###Connect to the Redux |
| 54 | +```js |
| 55 | +// store/reducer.js |
| 56 | +import {createReducer }from'redux'; |
| 57 | +importcommentsReducerfrom'./comments'; |
| 58 | + |
| 59 | +exportdefaultcreateReducer({ |
| 60 | +/* Convert "Reducer" class into pure function*/ |
| 61 | + comments:commentsReducer.toFunction() |
| 62 | +}); |
| 63 | +``` |
37 | 64 |
|
38 | 65 | ##Documentation |
39 | 66 | For more details on methods, usage examples and troubleshooting[see the Documentation](./docs). |
|