showing results for - "redux saga fetch data using axios"
Charlotte
01 Jan 2021
1// action type
2const FETCH_ALL = "FETCH_ALL";
3const FETCH_FAIL = "FETCH_FAIL";
4
5// initial state
6const fetchState = {
7  users: [],
8  error: ""
9};
10
11//action creator
12const fetchDataAsync = () => {
13  return (dispatch) => {
14    axios
15      .get("https://jsonplaceholder.typicode.com/users")
16      .then(({ data }) => dispatch({ type: FETCH_ALL, users: data }))
17      .catch((err) => dispatch({ type: FETCH_FAIL, error: err }));
18  };
19};
20
21// reducer
22const fetchReducer = (state = fetchState, action) => {
23  switch (action.type) {
24    case FETCH_ALL:
25      return action.users;
26    case FETCH_FAIL:
27      return { ...state, error: action.error };
28    default:
29      return state.users;
30  }
31};
32
33// store
34const reducers = combineReducers({ users: fetchReducer });
35const store = createStore(reducers, applyMiddleware(logger, thunk));
36
37//fetchAllData component
38import React, { useEffect } from "react";
39import { connect, useDispatch } from "react-redux";
40import { fetchDataAsync } from "../redux/Action";
41
42const FetchData = (props) => {
43  
44  const dispatch = useDispatch();
45  
46  useEffect(() => {
47    dispatch(fetchDataAsync());
48  }, []);
49
50  return (
51    <>
52      <ul>
53        {props.users.map((user) => (
54          <li key={user.id}>
55            {user.name} | {user.email}
56          </li>
57        ))}
58      </ul>
59    </>
60  );
61};
62
63const mapStateToProps = (state) => {
64  return { ...state };
65};
66
67export default connect(mapStateToProps)(FetchData);
Elias
30 Jun 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)
queries leading to this page
axios dispatch actioncan you put axios in reducerreact native redux api call exampleredux post request exampleredux thunk async axios exampleredux saga with axiosreact redux making multiple api callshow to use axios in redux sagareact redux axios in reducerreact redux axiosaxios delete example redux thunkpost data in react reduxreact redux and axiosredux sagas fetch callredux fetch data call api from redux in reactaxio with reduxfetch data from firebase using redux sagausing axios with reduxfetch api data using redux redux saga and axiosdata fetching with hard coded in redux appsgetting data from json server using reduxfetch api with redux saga coderedux thunk axios gethttp get action in react axioshow to track request post data with redux redux fetch jsonfetch data with reduxextrating values from reducer payloadfetch data in reduxredux load data from apiredux and axiosreact native redux axioshow to fetch data using axios with reduxhow to fetch data using axios in reduxreact redux fetch data from api examplereact native fetch data using axios in reduxfetch request not working in redux actionwhat is dispatch in axiosfetch data in react redux in consolefetch api with redux sagareact native redux api callany method in redux which immediately shows something posted in the backend in reactfetch api with redux saga code functionpost api response is completed react native redux sagamake redux third party api call with custom functionredux fetch data on loadadding fetch in reduxrefactor axios rest operations to reduxupdate store in redux on axios postaxios or thunk which oneredux saga with fetch callget database response with reduxangilar fetching state reduxfetch redux thunk display datahow to fetch data from axios in reduxreact redux and axios best setupfetch data use reduxsaga fetch api example without using axiosfetch data from api via redux sagaredux saga call fetchaxios send body in post request in reduxhow to make database calls in redux stateredux saga fetchreact redux example with api callsredux fetch datareact native redux fetch data from apifetch data using redux sagareact redux load data from apihow to fetch data when the index js is ran reduxusing redux thunk vs axios in react compontentredux saga fetch without axiofetch data from api using redux sagaaxios with react reduxredux same http request handlingredux thunk axios postreact api call axios reduxfetch api data using redux sagareact redux fetch axioshow to api payload response showing on network for redux in react js 3fapi calling using redux in reactredux api call exampleredux saga fetch data from apihow to get the callback nof any api using redux in react nativecall payload in redux examplefetch data with redux sagahow to call api by axios from action in redux reactfetch vs axios with redux sagaredux axios exampleredux payload objectredux axios middlewarehow redux thunk works differently in react native debug mode issuereact redux axios post requestreact redux post request example implement fetch using redux sagaredux thunk axiosaxios with reduxreact native redux react redux axios projectstore data from axios to redux with idreact native redux react redux axiosusing rest api and saving in db react native reduxuse axios with action creatormake api call in react reduxreact native redux api callfetch success reducerreact redux api clientfetch user redux examplereact redux with axioswhat types of data should i fetch in reducredux sagas fetch apireact 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 sagaredux action axiosredux saga fetch data using axiosaxios not fetching in redux thunkpost the data redux to apiredux saga fetch retryredux with axiosredux fetch data using axiosaxios react reduxupdate store redux data on axios postsaga fetch api example without axiosaxios data reduxbest way to fetch data using axios in react reduxredux saga fetch reactputting axios in reducerhow to get axios instance into redux statereact redux saga fetch dataredux axios api exampleaxios in reducerredux thunk vs axiosredux api call using thunk and axiosfetch api using axios in reduxpost request axios with reduxreact reducer axiosreact redux connect axios fetch data from api examplereact redux fetch data from api not workingreact redux store axios datausing axios in redux react calling apiredux axioshow to post and get data with redux reactaxios fetch data redux actionredux http request exampledispatch in react get axios datafetching data from api redux sagaexample redux axiosredux fetch api middlewareredux vs axiosredux axios thunk getuse fetch inside sagaredux not getting api dataaxios and reduxhow to store data in redux store after get from axiosredux thunk post requestaxios get on react with reduxsagas for fetch apiloading 2c data 2c error axios api all with reduxstore data from axios to reduxhow to fetch data usinng axios with reduxgetting data from axios using react hooksredux fetch data is in action payloadaxios and redux functional componentsreducer load data from apihow to fetch data using axios in react reduxredux saga fetch datasign up redux fetch requestfetch data reduxuse axios with react reduxredux saga fetch data using axios