redux-async-toolkit
Library that helps you write async code in redux style without need to write a lot of reducers. Simply use the one included in this library.
If you are curious about usage scroll down to 4th point.
Installation
To install the current version:
npm install --save redux-async-toolkit
1. Attach reducer
;; const reducer = ;
2. Attach middleware to your store
;; const client = getHello: ;; const finalCreateStore = createStore; const store finalCreateStorereducer;
3a. Create some actions
{ return key: 'hello' //points to the path in the store client ;}
3b. Cache your results if needed
{ return key: 'hello' //points to the path in the store client cache: forceReload ? false : 10 // cache duration in seconds ;}
4. Connect as usual
@ static propTypes = hello: PropTypesobject // ... so you have to define its type as object here ... loadHello: PropTypesfuncisRequired ; { thisprops; // ... call your action on after component mount or whenever you want ... } { const hello = thisprops; return <div> hellopending ? // 'pending' prop is set to true at each action start <div>Loading ...</div> : <div> helloready && <div>hellodata</div> // 'ready' means that at least one Promise was resolved helloerror && <div>helloerror</div> // 'error' contains data passed to Promise reject </div> </div> ; }
License
MIT