Install
npm install --save redux-thunk-status
Usage
-
First you'll need to ensure
redux-thunk
middleware is applied to store already:const store = -
Install the reducer into the
async
path of yourcombineReducers
:const reducer = -
Create asynchronous actions/thunks (These must return a promise):
// Thunk returning promiseconst getDataFromPromise = {return {return {}}}// or with ajaxconst getDataFromAjaxCall = {return {return axios})}} -
Name and wrap dispatchable actions with the
captureStatus( name, action )
method:const mapDispatchToProps = {return{}} -
Get status of thunk with the
getLoadingStatus( name )( state )
selector method:const mapStateToProps = {returnloading: state} -
Optionally use the
AwaitLoading
component to conditionally render jsx blocks or components:// Thunk returning promiseconst getDataFromPromise = {return {return {}}Component{super props}{thisprops}{const items loading = thispropsreturn<div><AwaitLoading loading=loading><ul>items}const mapStateToProps = {returnitems: stateitemsloading: state}const mapDispatchToProps = {return{}}mapStateToProps mapDispatchToProps ItemsYou can also pass a custom loading component to the
component
prop ofAwaitLoading
:const RenderLoading = {return<div><i className="fa fa-refresh fa-spin fa-3x fa-fw"></i><span className="sr-only">Loading...</span></div>}<AwaitLoading loading=loading component=RenderLoading>/* child jsx */</AwaitLoading>