2017-01-02 20 views
0

我遇到了一個問題,看起來像一個簡單和非常常見的使用情況爲REDX傳奇。我正在嘗試調用API來爲我的redux商店填充一組初始數據。要做到這一點,我試圖在啓動時運行一個傳奇。薩加斯不觸發其他傳奇和行動

store.js的相關部分看起來像這樣:

const reduxRouterMiddleware = routerMiddleware(browserHistory) 
const sagaMiddleware = createSagaMiddleware() 
const middleware = [reduxRouterMiddleware, sagaMiddleware] 

const enhancers = compose(
    window.devToolsExtension ? window.devToolsExtension() : f => f, 
    applyMiddleware(...middleware) 
) 

const store = createStore(rootReducer, defaultState, enhancers) 

sagaMiddleware.run(rootSaga) 

export const history = syncHistoryWithStore(browserHistory, store) 
export default store 

sagas.js文件看起來像這樣:

export function* fetchListings() { 
    try { 
    console.log('fetchListings saga - try') 
    const response = yield call(api.get, '/api/listings') 
    console.log(response.data.listings) 
    yield put({type: 'FETCH_LISTINGS_SUCCESS', listings: response.data.listings}) 
    } 
    catch (err) { 
    console.log('fetchListings saga - error') 
    yield put({type: 'FETCH_LISTINGS_ERROR', error: err}) 
    } 
} 

export function* listenFetchListings() { 
    yield takeEvery('FETCH_LISTINGS_REQUEST', fetchListings) 
    console.log('watcher saga') 
} 

export default function* rootSaga() { 
    yield fork (fetchListings) 
} 

頁面加載後,我可以看到請求API被解僱和控制檯日誌顯示預期的有效負載回來。不幸的是,FETCH_LISTINGS_SUCCESS行動沒有得到調度。當我從ReduxDevTools手動調度它時,它會很好地修改我的狀態。 出於測試目的,我還添加了listenFetchListings傳奇,嘗試用明確的電話進行測試。不幸的是,當我手動調度FETCH_LISTINGS_REQUEST動作時,它似乎也不會被觸發。 經過幾個小時的測試,閱讀文檔和教程我不得不承認失敗。請幫忙。

回答

0

其實,這個問題有正確的答案放在爲註釋: redux-saga cannot catch actions dispatched by store enhancers

的問題是,爲構建功能參數的順序。 當這樣組成時,完美的作品:

const enhancers = compose(
    applyMiddleware(...middleware), 
    window.devToolsExtension ? window.devToolsExtension() : f => f 
)