question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding support for dispatching Sagas from Action Creators (like in redux-thunk)

See original GitHub issue

Is there a functionality we lose if we would simply do dispatch(mySaga) instead of registering all the sagas in middleware on the store creation step

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:24 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
kloycommented, Feb 27, 2016

@yelouafi Thank you for the example. Below I have pasted a working middleware.

function isGenerator(fn) {
  return typeof fn === 'function' && 'next' in fn.prototype && 'throw' in fn.prototype;
}

const createSagaDispatcher = sagaMiddleware => () => next => action => {
  if (isGenerator(action)) {
    return sagaMiddleware.run(action).done;
  }

  return next(action);
};

export default createSagaDispatcher;

Here is an example of dispatching a generator…

https://github.com/kloy/saga-playground/blob/master/src/index.js#L33

This is not meant as a good example of when to dispatch a generator, just a way for me to prove the functionality.

0reactions
yelouaficommented, Mar 1, 2016

I’ll close this. As I don’t plan to add dispatching to the core. Let me know If someone would like to contribute a separate middleware for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Different Ways to Dispatch Actions with Redux
In this guide, we'll look at different ways of dispatching the action and at isolating the components from Redux.
Read more >
Understanding Redux Saga: From action creators to sagas
Redux Thunk allows an action creator to dispatch a function in addition to a plain object, converting the action creator into a thunk....
Read more >
Understanding Asynchronous Redux Actions with ...
Learn how to use the Redux Thunk middleware to run asynchronous operations, talk to an API and dispatch actions to the store.
Read more >
Writing Logic with Thunks
Each thunk typically requires defining three different action types + matching action creators for "pending/fulfilled/rejected", plus the actual ...
Read more >
Understanding redux-saga: From action creators to sagas
Since the middleware automatically passes the dispatch function to the function that the action creator returns, for the component, there will ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found