redux saga fetch api

Solutions on MaxInterview for redux saga fetch api by the best coders in the world

showing results for - "redux saga fetch api"
Juan Esteban
02 Aug 2016
1//EXAMPLE FETCH DATA API REDUX SAGA
2
3// USER ACTION CREATOR
4export const REQUEST_API_DATA = 'REQUEST_API_DATA'
5export const RECEIVE_API_DATA = 'RECEIVE_API_DATA'
6
7export const requestApiData = () => ({ type: REQUEST_API_DATA })
8
9// USER REDUCER
10import { REQUEST_API_DATA, RECEIVE_API_DATA } from '../actions/user'
11
12export default (state = {}, { type, payload }) => {
13  switch (type) {
14    case RECEIVE_API_DATA:
15      return payload.users
16    default:
17      return state
18  }
19}
20
21// USER SAGA
22import axios from 'axios'
23import { call, put, takeEvery, takeLatest } from 'redux-saga/effects'
24import { REQUEST_API_DATA, RECEIVE_API_DATA } from './actions/user'
25
26function* userReceiveAll(action) {
27  try {
28    const { data } = yield call(axios.get, 'https://jsonplaceholder.typicode.com/users')
29    yield put({ type: RECEIVE_API_DATA, payload: { users: data } })
30  } catch (e) {
31    console.log(e.message)
32  }
33}
34export default function* userSendAll() {
35  yield takeLatest(REQUEST_API_DATA, getApiData)
36}
37
38// REDUX STORE
39import { createStore, applyMiddleware, combineReducer } from 'redux'
40import createSagaMiddleware from 'redux-saga'
41import { all } from 'redux-saga/effects'
42import logger from 'redux-logger'
43import userReducer from './reducers/user'
44import userSaga from './sagas/user'
45
46function* saga() {
47  yield all([userSaga()]) 
48}
49
50export const store = () => {
51 const sagaMiddleware = createSagaMiddleware()
52 const store = createStore(combineReducer({users: userReducer}), 
53 applyMiddleware(sagaMiddleware, logger)) 
54 sagaMiddleware.run(saga)
55 return store;
56}
57
58// USER COMPONENT
59import React from 'react'
60import { connect } from 'react-redux'
61import { requestApiData } from './actions'
62
63class User extends React.Component {
64  componentDidMount() {
65    this.props.fetchAll()
66  }
67  render() {
68    const { users } = this.props.state
69    const results = users.length > 0 ? users : []
70    return (
71      <div>
72        {results.map((v) => (
73          <ul key={v.id}>
74            <li>{v.username}</li>
75          </ul>
76        ))}
77      </div>
78    )
79  }
80}
81
82const mapStateToProps = (state) => ({ state })
83const mapDispatchToProps = (dispatch) => ({ fetchAll: () => dispatch(requestApiData()) })
84
85export default connect(mapStateToProps, mapDispatchToProps)(User)
Wesley
28 Jan 2016
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}
queries leading to this page
redux saga fetchingrefactor axios rest operations to reduxpost api response is completed react native redux sagahow to hit api in redux saga on load of pageredux payload objectaxios dispatch loghow to fetch data using redux sagareact redux saga rest api call when component loadsredux http request examplehow redux thunk works differently in react native debug mode issueredux saga fetch reactaxios react reduxredux sagas fetch callredux saga with axiossaga fetch api example without axiosfetch api data using redux sagaimplement fetch using redux sagaredux saga fetchget api call using sagasredux saga fetch without axiofetch data using redux sagahow to post call and add to state redux sagaredux load data from apiredux saga with fetch callsaga fetch api example without using axiosangilar fetching state reduxredux saga fetch data using axiosfetching data from api redux sagareact redux saga fetch datafetch data from api via redux sagafetch request not working in redux actionapi integration in redux sagasagas for fetch apiaxio with reduxredux saga with class to fetch dataaxios with reduxhow to fetch data using axios with reduxfetch api data using redux redux saga and axiosredux saga fetch data from apiuse fetch inside sagastore data from axios to redux with idhow to fetch data from axios in reduxredux saga call fetchaxios fetch data redux actionredux fetch jsonfetch data from api using redux sagahow to fetch data using axios in reduxredux saga fetch jsonfetch api with redux saga code functioncall api from redux in reactreact redux axiosreact redux load data from apibest way to fetch data using axios in react reduxhow to write api call in redux saga file 3fredux saga fetch apiredux thunk post requestcase 27news received 27 3a return 7b state 2c news 3a action json 5b0 5d 2c loading 3a false 7dreact site where data is fetch with the help of axios and redux 2c sagas and shown in the cardsfetch api with redux sagafetch data with redux sagafetch data from firebase using redux sagaredux saga fetch datafetch api with redux saga codehow to fetch data usinng axios with reduxusing redux thunk vs axios in react compontentfetch vs axios with redux sagareact redux saga api examplereact redux axios post requestreact native redux api call exampleredux fetch data using axiosredux sagas fetch apiredux saga fetch retryfetch data in redux saga from apihow to post and get data with redux reacthow to use axios in redux sagaredux saga fetch api