在我的redux項目中,我想在每個動作分派中檢查一些東西(例如網絡連接)。我應該實現使用它接受所有類型的動作(沒有類型檢查)減速低於Redux中間件和Reducer接受所有動作的區別
export default (state = defaultState) => ({
...state,
neworkStatus: navigator.onLine
})
或中間件給出。
const NetworkMiddleware = store => next => (action) => {
const result = next(action)
const state = store.getState()
if (navigator.onLine && !state.NetworkDetector.networkStatus) next({ type: 'NETWORK_SUCCESS' })
if (!navigator.onLine && state.NetworkDetector.networkStatus) next({ type: 'NETWORK_ERROR' })
return result
}
export default NetworkMiddleware;
的是這兩種實現