Slide 1

Slide 1 text

Lidando com Efeitos Colaterais com Redux Saga @filipebarcos

Slide 2

Slide 2 text

Redux

Slide 3

Slide 3 text

"Evolui ideias do Flux, mas evita suas complexidades pegando dicas do Elm" — github.com/reactjs/redux Redux

Slide 4

Slide 4 text

Redux UI Reducer Store Action dispatched (currentState, action) => newState New State

Slide 5

Slide 5 text

Redux UI Middleware Reducer Store Action dispatched (currentState, action) => newState New State Action forwarded Action dispatched

Slide 6

Slide 6 text

Redux Saga

Slide 7

Slide 7 text

github.com/redux-saga/redux-saga

Slide 8

Slide 8 text

JavaScript Generator Functions*

Slide 9

Slide 9 text

ES6

Slide 10

Slide 10 text

Podemos paralisar a execução da função

Slide 11

Slide 11 text

function* foo(x) { const y = 2 * (yield (x + 1)); const z = yield (y / 3); return (x + y + z); } var it = foo(5); // note: not sending anything into `next()` here console.log(it.next()); // { value:6, done:false } console.log(it.next(12)); // { value:8, done:false } console.log(it.next(13)); // { value:42, done:true } https://davidwalsh.name/es6-generators

Slide 12

Slide 12 text

import { call, put } from 'redux-saga/effects'; export function* fetchData(action) { try { const data = yield call(Api.fetchUser, action.payload.url); yield put({type: "FETCH_SUCCEEDED", data}); } catch (error) { yield put({type: "FETCH_FAILED", error}); } } function* watchFetchData() { yield takeEvery('FETCH_REQUESTED', fetchData); } https://redux-saga.github.io/redux-saga/docs/basics/UsingSagaHelpers.html

Slide 13

Slide 13 text

import { takeEvery } from 'redux-saga'; // FETCH_USERS function* fetchUsers(action) { ... } // CREATE_USER function* createUser(action) { ... } // use them in parallel export default function* rootSaga() { yield takeEvery('FETCH_USERS', fetchUsers); yield takeEvery('CREATE_USER', createUser); } https://redux-saga.github.io/redux-saga/docs/basics/UsingSagaHelpers.html

Slide 14

Slide 14 text

github.com/filipebarcos/redux-saga-example

Slide 15

Slide 15 text

gist.github.com/filipebarcos/ 0a137d6ca837c117f999958a365fc5b6

Slide 16

Slide 16 text

Obrigado. @filipebarcos