Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Have Redux-Saga and Redux-Thunk Working Together?

Tags:

I was working with redux-saga but I'm with a problem: the redux-auth-wrapper needs the redux-thunk to do the redirects, so I simply added the thunk in my store:

  import {createStore, compose, applyMiddleware} from 'redux';   import createLogger from 'redux-logger';   import {routerMiddleware} from 'react-router-redux';   import {browserHistory} from 'react-router';   import thunk from 'redux-thunk';   import createSagaMiddleware, {END} from 'redux-saga';   import sagas from '../sagas';   import reduxImmutableStateInvariant from 'redux-immutable-state-invariant';   import rootReducer from '../reducers';   import _ from 'lodash';   import {loadState, saveState} from '../connectivity/localStorage';    const persistedState = loadState();    const routerMw = routerMiddleware(browserHistory);   const loggerMiddleware = createLogger();   const sagaMiddleware = createSagaMiddleware();    function configureStoreProd() {     const middlewares = [       // Add other middleware on this line...        routerMw,       sagaMiddleware,       thunk         ];      const store = createStore(rootReducer, persistedState, compose(       applyMiddleware(...middlewares)       )     );      store.subscribe(_.throttle(() => {       saveState({         auth: store.getState().auth       });     }, 1000));      sagaMiddleware.run(sagas);     store.close = () => store.dispatch(END);      return store;   }    function configureStoreDev() {     const middlewares = [       // Add other middleware on this line...        // Redux middleware that spits an error on you when you try to mutate your state either inside a dispatch or between dispatches.       reduxImmutableStateInvariant(),        routerMw,       sagaMiddleware,       loggerMiddleware,       thunk     ];      const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; // add support for Redux dev tools     const store = createStore(rootReducer, persistedState, composeEnhancers(       applyMiddleware(...middlewares)       )     );      store.subscribe(_.throttle(() => {       saveState({         auth: store.getState().auth       });     }, 1000));      if (module.hot) {       // Enable Webpack hot module replacement for reducers       module.hot.accept('../reducers', () => {         const nextReducer = require('../reducers').default; // eslint-disable-line global-require         store.replaceReducer(nextReducer);       });     }      sagaMiddleware.run(sagas);     store.close = () => store.dispatch(END);      return store;   }    const configureStore = process.env.NODE_ENV === 'production' ? configureStoreProd : configureStoreDev;    export default configureStore; 

This way works nice without errors, but I'm new in react and I don't know if have problem with redux-saga and redux-thunk working together....

Someone can help me?

like image 816
bugalaws Avatar asked Oct 14 '17 02:10

bugalaws


People also ask

Which is better Redux thunk or Redux Saga?

But on the other hand, for bigger projects, Redux-Thunk may sometimes get you in trouble, as it can be hard to scale if your side effect or asynchronous logic increases, whereas in the case of Redux-Saga, it comes power-packed with some amazing things such as concurrent side effects, canceling side effects, debouncing ...

Can I use Redux toolkit with Saga?

Redux-Saga is one of the popular middleware libraries for Redux. It makes working with asynchronous operations and side effects a lot easier. Unlike other libraries like Redux Thunk, Redux-Saga uses ES6 generators. Therefore, you should be knowledgeable in ES6 generators to implement Sagas correctly.

Is Redux thunk still used?

Thunks are the standard approach for writing async logic in Redux apps, and are commonly used for data fetching. However, they can be used for a variety of tasks, and can contain both synchronous and asynchronous logic.

Is Redux Saga obsolete?

Such a powerful & elegant tool as Redux-Saga, a Redux side effect manager, is said to be deprecated, and no longer being maintained, starting from Jan 27, 2021.


1 Answers

No problems to have both. Sagas are just background checkers who react to some actions while thunk let's you have more interesting action creators.

While thunk will act more like synced code, sagas will do it's job in a background.

Both extensions do not change how actions are flying around. Actions still, in the end, are just bare objects like w/o thunk or w/o sagas.

like image 118
Lukas Liesis Avatar answered Oct 02 '22 14:10

Lukas Liesis