Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
omichelsen committed Aug 23, 2019
1 parent 8467c3d commit 96b1b3f
Show file tree
Hide file tree
Showing 11 changed files with 1,399 additions and 55 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# [2.1.0](https://github.com/omichelsen/redux-promise-middleware-actions/compare/v2.0.0...v2.1.0) (2019-08-21)


### Bug Fixes

* **test:** add rejected reason ([375b379](https://github.com/omichelsen/redux-promise-middleware-actions/commit/375b379))


### Features

* **meta:** add metadata support ([3643edf](https://github.com/omichelsen/redux-promise-middleware-actions/commit/3643edf))



# [2.0.0](https://github.com/omichelsen/redux-promise-middleware-actions/compare/b3f843d...v2.0.0) (2018-08-16)


### Features

* **ts:** use generic rest params with TS 3.0 ([b3f843d](https://github.com/omichelsen/redux-promise-middleware-actions/commit/b3f843d))



28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ Note that if you try and use the base function in a reducer, an error will be th
case String(fetchData): // throws an error
```

### Reducer

To create a type safe reducer `createReducer` takes a list of handlers that accept one or more actions and returns the new state.

```js
import { createAction, createReducer } from 'redux-promise-middleware-actions';

const increment = createAction('INCREMENT');
const decrement = createAction('DECREMENT');
const reset = createAction('RESET', (count: number) => count);

const defaultState = 0;

const reducer = createReducer(defaultState, handleAction => [
handleAction(increment, state => state + 1),
handleAction(decrement, state => state - 1),
handleAction(reset, (_state, { payload }) => payload),
]);

reducer(undefined, increment()); //=> 1
reducer(undefined, decrement()); //=> -1
reducer(3, reset(0)); //=> 0
```

### Async reducer

You can now handle the different events in your reducer by referencing the possible outcome states:
Expand Down Expand Up @@ -212,3 +236,7 @@ dispatch(foo());
// { type: 'FETCH_DATA/FULFILLED', payload: ... }
// { type: 'FETCH_DATA/REJECTED', payload: ... }
```

## Acknowledgements

Thanks to [Deox](https://github.com/thebrodmann/deox/) for a lot of inspiration for the TypeScript types.
Loading

0 comments on commit 96b1b3f

Please sign in to comment.