showing results for - "redux saga fetch json"
Stefano
01 Mar 2018
1// USER ACTION CREATOR
2export const userState = {};
3export const RECEIVED_ALL = "RECEIVED_ALL";
4export const REQUEST_ALL = "REQUEST_ALL";
5
6export const userRequestAllActionCreator = (type) => ({
7  type: type
8});
9
10
11// USER REDUCER
12import { userState, RECEIVED_ALL } from "../actions/user";
13
14export const userReducer = (state = userState, action) => {
15  switch (action.type) {
16    case RECEIVED_ALL:
17      return action.payload;
18    default:
19      return state;
20  }
21};
22
23// USER SAGA
24import axios from "axios";
25import { put, takeLatest } from "redux-saga/effects";
26import { REQUEST_ALL, RECEIVED_ALL } from "../actions/user";
27
28function* userReceiveAll() {
29  const { data } = yield axios.get("https://jsonplaceholder.typicode.com/users");
30  yield put({ type: RECEIVED_ALL, payload: data });
31}
32
33export function* userSagaAll() {
34  yield takeLatest(REQUEST_ALL, userReceiveAll);
35}