Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

redux saga fetch data

//EXAMPLE FETCH DATA API REDUX SAGA

// USER ACTION CREATOR
export const REQUEST_API_DATA = 'REQUEST_API_DATA'
export const RECEIVE_API_DATA = 'RECEIVE_API_DATA'

export const requestApiData = () => ({ type: REQUEST_API_DATA })

// USER REDUCER
import { REQUEST_API_DATA, RECEIVE_API_DATA } from '../actions/user'

export default (state = {}, { type, payload }) => {
  switch (type) {
    case RECEIVE_API_DATA:
      return payload.users
    default:
      return state
  }
}

// USER SAGA
import axios from 'axios'
import { call, put, takeEvery, takeLatest } from 'redux-saga/effects'
import { REQUEST_API_DATA, RECEIVE_API_DATA } from './actions/user'

function* userReceiveAll(action) {
  try {
    const { data } = yield call(axios.get, 'https://jsonplaceholder.typicode.com/users')
    yield put({ type: RECEIVE_API_DATA, payload: { users: data } })
  } catch (e) {
    console.log(e.message)
  }
}
export default function* userSendAll() {
  yield takeLatest(REQUEST_API_DATA, getApiData)
}

// REDUX STORE
import { createStore, applyMiddleware, combineReducer } from 'redux'
import createSagaMiddleware from 'redux-saga'
import { all } from 'redux-saga/effects'
import logger from 'redux-logger'
import userReducer from './reducers/user'
import userSaga from './sagas/user'

function* saga() {
  yield all([userSaga()]) 
}

export const store = () => {
 const sagaMiddleware = createSagaMiddleware()
 const store = createStore(combineReducer({users: userReducer}), 
 applyMiddleware(sagaMiddleware, logger)) 
 sagaMiddleware.run(saga)
 return store;
}

// USER COMPONENT
import React from 'react'
import { connect } from 'react-redux'
import { requestApiData } from './actions'

class User extends React.Component {
  componentDidMount() {
    this.props.fetchAll()
  }
  render() {
    const { users } = this.props.state
    const results = users.length > 0 ? users : []
    return (
      <div>
        {results.map((v) => (
          <ul key={v.id}>
            <li>{v.username}</li>
          </ul>
        ))}
      </div>
    )
  }
}

const mapStateToProps = (state) => ({ state })
const mapDispatchToProps = (dispatch) => ({ fetchAll: () => dispatch(requestApiData()) })

export default connect(mapStateToProps, mapDispatchToProps)(User)
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery function return 
Javascript :: how to practice javascript 
Javascript :: anonymous functions javascript 
Javascript :: classlist.contain in javascript 
Javascript :: dom event 
Javascript :: htmlfor jsx attr 
Javascript :: jquery find and replace text 
Javascript :: jscrollpane set background color 
Javascript :: array destructuring 
Javascript :: axios npm 
Javascript :: .pop js 
Javascript :: how to get user info from google oauth node js 
Javascript :: how to repeat string in javascript 
Javascript :: react big calendar messages 
Javascript :: trim string 
Javascript :: how the concat function works javascript 
Javascript :: js start at this character 
Javascript :: adding a if stement in jsx 
Javascript :: react native map 
Javascript :: includes() js 
Javascript :: firebase functions add to database 
Javascript :: video conferencing app with html and js 
Javascript :: javascript email validation 
Javascript :: combine 2 "arrays with objects" and remove object duplicates javascript 
Javascript :: difference between ajax and node js 
Javascript :: deleteicon material ui improt 
Javascript :: expo react navigation 
Javascript :: inheritance in class in js 
Javascript :: xpath in javascript 
Javascript :: database in javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =