showing results for - "redux fetch data using axios"
Barclay
26 Mar 2017
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);
Juan David
31 Apr 2020
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
how to fetch data using redux sagareact native redux api callredux thunk axios postreact redux fetch data from api examplefetch data with reduxfetch api with redux saga code functionredux saga fetch dataredux http request examplebest way to fetch data using axios in react reduxredux axioshow to call api by axios from action in redux reactaxios dispatch logaxios not fetching in redux thunkredux same http request handlingreact native fetch data using axios in reduxreact redux store axios datahow to fetch data using axios with reduxredux thunk axios getfetch success reducerhow to get axios instance into redux stateredux api call using thunk and axiosfetch data from api using redux sagaaxios with reduxhow to track request post data with redux call payload in redux examplefetch api data using redux redux saga and axiosfetch data using redux sagareact redux and axiosexample redux axiosreact redux making multiple api callsfetch data reduxstore data from axios to reduxhow to fetch data when the index js is ran reduxredux fetch data how to use axios in redux sagaredux thunk post requestfetching data from api redux sagaredux saga fetch data from apireact redux post request example make redux third party api call with custom functionreact native redux api call exampleaxios react reduxredux load data from apireact redux with axiosreact redux and axios best setupdispatch in react get axios dataredux saga fetch without axiousing axios in redux react calling apiusing redux thunk vs axios in react compontentfetch user redux examplehow to get the callback nof any api using redux in react nativefetch data with redux sagaredux not getting api datarefactor axios rest operations to reduxfetch api data using redux sagaaxios and reduxreact native redux api callredux vs axiospost api response is completed react native redux sagapost request axios with reduxusing axios with reduxredux and axiosgetting data from axios using react hookspost data in react reduxupdate store in redux on axios postredux saga fetch retryfetch api with redux sagareact native redux axiosget database response with reduxredux fetch api middlewaresign up redux fetch requestmake api call in react reduxfetch redux thunk display datausing rest api and saving in db react native reduxaxio with reduxredux post request examplereducer load data from apireact redux fetch data from api not workingaxios and redux functional componentsredux fetch jsonreact redux example with api callsreact redux fetch axiosloading 2c data 2c error axios api all with reduxhttp get action in react axiosredux action axiosredux saga with axiosreact reducer axiosreact redux api clientredux saga call fetchredux saga with fetch callredux saga fetchaxios get on react with reduxreact site where data is fetch with the help of axios and redux 2c sagas and shown in the cardsany method in redux which immediately shows something posted in the backend in reactreact redux axios post requesthow to fetch data using axios in react reduxredux payload objectputting axios in reducerfetch data use reduxaxios fetch data redux actionredux fetch data on loadfetch vs axios with redux sagahow to fetch data from axios in reduxaxios with react reduxredux fetch data using axiossaga fetch api example without using axioscan you put axios in reducerimplement fetch using redux sagahow redux thunk works differently in react native debug mode issuehow to store data in redux store after get from axiosredux axios middlewarehow to fetch data using axios in reduxredux sagas fetch callredux api call exampleangilar fetching state reduxredux fetch dataaxios or thunk which oneaxios in reduceradding fetch in reduxuse axios with react reduxfetch data in react redux in consolereact redux axios in reducerfetch data from api via redux sagaaxios send body in post request in reduxhow to fetch data usinng axios with reduxfetch data in reduxaxios data reduxsaga fetch api example without axiosfetch data from firebase using redux sagahow to post and get data with redux reactredux axios examplereact native redux fetch data from apiredux saga fetch reactreact redux connect axios fetch data from api examplereact redux load data from apipost the data redux to apisagas for fetch apiupdate store redux data on axios postuse fetch inside sagause axios with action creatoraxios delete example redux thunkredux thunk vs axiosdata fetching with hard coded in redux appsreact native redux react redux axios projectwhat types of data should i fetch in reducredux axios api examplereact api call axios reduxhow to make database calls in redux statefetch api using axios in reduxredux sagas fetch apihow to api payload response showing on network for redux in react js 3fredux saga fetch data using axioswhat is dispatch in axiosapi calling using redux in reactredux thunk axiosfetch request not working in redux actiongetting data from json server using reduxcall api from redux in reactstore data from axios to redux with idaxios dispatch actionreact native redux react redux axiosreact redux saga fetch dataredux with axiosextrating values from reducer payloadfetch api with redux saga coderedux thunk async axios examplereact redux axiosredux axios thunk getredux fetch data is in action payloadredux fetch data using axios