showing results for - "redux saga fetch data"
Diego
15 Jan 2021
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)
queries leading to this page
react redux axios post requestimplement fetch using redux sagareact native redux api call exampleaxios with reduxredux saga with axiosstore data from axios to redux with idredux saga with fetch callhow to use axios in redux sagaangilar fetching state reduxreact redux axioshow to post and get data with redux reactaxios fetch data redux actionredux http request examplehow to fetch data from axios in reduxsaga fetch api example without using axiosredux sagas fetch callfetch data from api via redux sagaredux saga call fetchcall api from redux in reactaxio with reduxfetching data from api redux sagafetch data from firebase using redux sagaredux saga fetchfetch api data using redux redux saga and axiosredux sagas fetch apifetch api with redux saga codereact site where data is fetch with the help of axios and redux 2c sagas and shown in the cardsaxios dispatch loghow to fetch data using redux sagause fetch inside sagafetch data using redux sagareact redux load data from apipost api response is completed react native redux sagausing redux thunk vs axios in react compontentredux saga fetch data using axiosredux saga fetch without axioredux load data from apifetch data from api using redux sagaredux saga fetch retryredux thunk post requesthow to fetch data using axios with reduxhow to fetch data using axios in reduxsagas for fetch apifetch request not working in redux actionredux fetch data using axioshow to fetch data usinng axios with reduxfetch api data using redux sagaaxios react reduxsaga fetch api example without axiosbest way to fetch data using axios in react reduxredux saga fetch reactfetch api with redux sagafetch api with redux saga code functionredux saga fetch data from apifetch data with redux sagafetch vs axios with redux sagareact redux saga fetch dataredux saga fetch dataredux payload objecthow redux thunk works differently in react native debug mode issuerefactor axios rest operations to reduxredux saga fetch data