2017-10-14 93 views
1

我和終極版,傳奇的工作,但我有一個問題:終極版認證 - 包裝需要了Redux-的thunk做重定向,所以我只是簡單地添加在我的商店在thunk:可以使用Redux-Saga和Redux-Thunk嗎?

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; 

這種方式工作很好,沒有錯誤,但我是新的反應,我不知道是否有問題與REDX傳奇和REDEX - thunk一起工作....

有人可以幫助我嗎?

+0

不用擔心,沒有衝突。確實,只有當動作是一個函數而不是一個文字對象時,'redux-thunk'纔會介入。 在使用redux-thunk和redux-saga之間不應該有任何衝突。 –

回答

0

沒有問題都有。薩加斯只是一些背景檢查者,他們會對一些行爲做出反應,而讓他們有更多有趣的動作創造者。

儘管thunk將更像同步代碼,但sagas會在後臺完成它的工作。

兩個擴展都不會改變動作如何飛行。最後,行動仍然只是一些沒有形式的東西,比如沒有東西或者沒有傳統。

相關問題