Last active
November 3, 2019 16:01
-
-
Save santospatrick/77b7c77066331e52cee61678ddec600d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------------------ | |
// 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