Redux Sagas
Redux Sagas
Component
Reducer
Action, State
State
UX
Props
Redux dispatcher
Redux Connect
Pure
In reducer
or in component ?
Redux Sagas
Redux-saga
Reducer
Action, State
Redux-saga
- Watch the dispatched actions
- Perform the async/impure actions
- Dispatch another action
Redux dispatcher
Redux-saga
Redux-saga
Reducer
Action, State
Redux-saga
Redux dispatcher
Component
State
UX
Props
Redux Connect
Redux-saga
function* mySaga () {
yield take(LOAD_DATA)
result = yield call(fetch_etc)
yield put(DATA_LOADED, result)
}
Redux-saga
redux-saga is a library that aims to make application side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, simple to test, and better at handling failures
(from the documentation)
Redux-saga
"asynchronous things like data fetching and impure things like accessing the browser cache"
=>
Redux-saga
Redux-saga
// Writing to console
function foo() {
console.log('Hello World');
}
// Ajax calls
function bar() {
fetch('some-url').then('display it');
}
Redux-saga
Redux-saga
// location
function foo() {
location.href = 'http://google.com';
}
// localStorage
function bar() {
localStorage.set('x', 1)
}