} from 'redux' import todos from './reducers/todos' import other from './reducers/other' const rootReducer = combineReducers({ todos, other }) const store = createStore(rootReducer)
side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, easy to test, and better at handling failures.” The mental model is that a saga is like a separate thread in your application that's solely responsible for side effects. redux-saga is a redux middleware, which means this thread can be started, paused and cancelled from the main application with normal redux actions, it has access to the full redux application state and it can dispatch redux actions as well.
'redux' import todos from './reducers/todos' import other from './reducers/other' const rootReducer = combineReducers({ todos, other }) const store = createStore(rootReducer)
'redux' import createSagaMiddleware from 'redux-saga' import todosReducer from './reducers/todos' import otherReducer from './reducers/other' import watchFetchTodos from './sagas/todos' const sagaMiddleware = createSagaMiddleware() const rootReducer = combineReducers({ todos, other }) const store = createStore( combineReducers(todosReducer, otherReducer), applyMiddleware(sagaMiddleware) ) // then run the saga sagaMiddleware.run(watchFetchTodos) // render the application
from 'redux' import createSagaMiddleware from 'redux-saga' import rootReducer from './reducers' import rootSaga from './sagas' const sagaMiddleware = createSagaMiddleware() const store = createStore( rootReducer applyMiddleware(sagaMiddleware) ) // then run the saga sagaMiddleware.run(rootSaga) // render the application
100s of unique async workflows, routing, API requests, due to composition, isolation of frontend side effects, and ease of testing. (Typescript types available too) - Easy to hot-reload in development as sagas are suspendible and replaceable. - Definitely not required for simple React/Redux apps, cognitive overhead and plenty of plumbing - Elm <=> Redux-loop (reducers return state and Cmd, middleware handles Cmd) - Async/Await is an option, but different testing strategy required - Can’t speak for redux-observable or RxJS