2017-07-06 68 views
1

好吧,所以從Hello World教程https://redux-saga.js.org/docs/introduction/BeginnerTutorial.html工作我的方式,我試圖建立一個非常簡單的fetch調用......只是我的生成器函數永遠得不到執行。Redux-Saga takeEvery不叫傳奇

相關的代碼如下所示,現在只是控制檯日誌用於測試目的:

export function* fetchData() { 
    debugger; 
    console.log('Fetching data'); 
} 

export function* fetchDataWatcher() { 
    console.log("watching"); 
    yield takeEvery('FETCH_REQUESTED', fetchData); 
}  

// single entry point to start all Sagas at once 
export default function* rootSaga() { 
    yield all([ 
    fetchDataWatcher() 
    ]); 
} 

當然,我實現了樣板在我index.js

import rootSaga from './sagas'; 
... 

const sagaMiddleware = createSagaMiddleware(); 
... 
const enhancers = 
    compose(
    window.devToolsExtension ? window.devToolsExtension() : f => f, 
    applyMiddleware(sagaMiddleware) 
); 

const store = createStore(
    combineReducers({ 
    ... 
    }), 
    defaultState, 
    enhancers 
); 

sagaMiddleware.run(rootSaga); 

在瀏覽器控制檯,我看到一個'看',但當我發出一個動作

{ 
type: 'FETCH_REQUESTED' 
} 

通過Redux的devtools,我希望調試器停止我的代碼,並通過fetchData函數指引我...但沒有任何反應。

我敢肯定我錯過了一些非常簡單的東西 - 你能幫我嗎?

編輯:

事實證明,如果我移動devTools增強劑的compose結束,一切工作正常。

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

現在我知道,造成的compose簽名依賴於提交給它的最後一個函數,這仍然是有些不清楚,我...有人可以澄清這是怎麼回事?

回答

1

嘗試以這種方式

export default function * root() { 
    yield [ 
    fetchDataWatcher() 
    ] 
} 
創建 rootSaga