Skip to content

Instantly share code, notes, and snippets.

@santospatrick
Last active November 3, 2019 16:01
Show Gist options
  • Save santospatrick/77b7c77066331e52cee61678ddec600d to your computer and use it in GitHub Desktop.
Save santospatrick/77b7c77066331e52cee61678ddec600d to your computer and use it in GitHub Desktop.
// ------------------
// SomeComponent.js
// ------------------
function SomeComponent() {
...
const getUser = id => {
dispatch({ type: '@user/FETCH_REQUESTED', id })
}
...
}
// ------------------
// sagas.js
// ------------------
import { takeEvery } from 'redux-saga/effects'
// Exemplo anterior
export function* fetchUser(action) {
const { data } = yield call(api.get, `/user/${action.id}`)
yield put({ type: "@user/FETCH_SUCCEEDED", data })
}
// Opa! Código novo!
function* watchFetchData() {
yield takeEvery('@user/FETCH_REQUESTED', fetchUser)
}
// ------------------
// reducer.js
// ------------------
export default function users(state = [], action) {
switch (action.type) {
case '@user/FETCH_SUCCEEDED':
return {
...state,
users: [...users, action.data]
}
default:
return state;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment