redux-saga-thunk
Dispatching an action handled by redux-saga returns promise. It looks like redux-thunk, but with pure action creators.
Component { // `doSomething` dispatches an action which is handled by some saga thisprops }
redux-saga-thunk
uses Flux Standard Action to determine action'spayload
,error
etc.
If you find this useful, please don't forget to star ⭐️ the repo, as this will help to promote the project.
Follow me on Twitter and GitHub to keep updated about this project and others.
Motivation
There are two reasons I created this library: Server Side Rendering and redux-form.
When using redux-saga on server, you will need to know when your actions have been finished so you can send the response to the client. There are several ways to handle that case, and redux-saga-thunk
approach is the one I like most. See an example.
With redux-form, you need to return a promise from dispatch
inside your submit handler so it will know when the submission is complete. See an example
Finally, that's a nice way to migrate your codebase from redux-thunk
to redux-saga
, since you will not need to change how you dispatch your actions, they will still return promises.
Install
$ npm install --save redux-saga-thunk
Basic setup
Add middleware
to your redux configuration (before redux-saga middleware):
^ const sagaMiddleware = const store = ^
Usage
You just need to set meta.thunk
to true
on your request actions and put it on your response actions inside the saga:
const action = type: 'RESOURCE_REQUEST' payload: id: 'foo' meta: thunk: true ^ // send the actionstore { whiletrue const payload meta = ^ try const detail = // payload == { id: 'foo' } catch e }
redux-saga-thunk
will automatically transform your request action and inject a key
into it.
You can also use it inside sagas with put.resolve
:
{ try const detail = put console catch error console }
Usage with selectors
To use pending
, rejected
, fulfilled
and done
selectors, you'll need to add the thunkReducer
to your store:
const reducer =
Now you can use selectors on your containers:
const mapStateToProps = loading: error: success: done:
API
Table of Contents
clean
Clean state
Parameters
Examples
const mapDispatchToProps =
pending
Tells if an action is pending
Parameters
Examples
const mapStateToProps = fooIsPending: barForId42IsPending: barForAnyIdIsPending: fooOrBazIsPending: fooOrBarForId42IsPending: anythingIsPending:
Returns boolean
rejected
Tells if an action was rejected
Parameters
Examples
const mapStateToProps = fooWasRejected: barForId42WasRejected: barForAnyIdWasRejected: fooOrBazWasRejected: fooOrBarForId42WasRejected: anythingWasRejected:
Returns boolean
fulfilled
Tells if an action is fulfilled
Parameters
Examples
const mapStateToProps = fooIsFulfilled: barForId42IsFulfilled: barForAnyIdIsFulfilled: fooOrBazIsFulfilled: fooOrBarForId42IsFulfilled: anythingIsFulfilled:
Returns boolean
done
Tells if an action is done
Parameters
Examples
const mapStateToProps = fooIsDone: barForId42IsDone: barForAnyIdIsDone: fooOrBazIsDone: fooOrBarForId42IsDone: anythingIsDone:
Returns boolean
License
MIT © Diego Haz