项目作者: DylanVann

项目描述 :
🏞 Redux Saga Request, request helper for Redux Saga.
高级语言: JavaScript
项目地址: git://github.com/DylanVann/redux-saga-request.git
创建时间: 2017-04-18T17:12:09Z
项目社区:https://github.com/DylanVann/redux-saga-request

开源协议:MIT License

下载


redux-saga-request

🏞 Redux Saga Request, request helper for Redux Saga.

npm
CircleCI
license

Helper for running async functions and dispatching actions in redux saga.

Usage

  1. yarn add redux-saga-request
  1. import { createRequest, request } from 'redux-saga-request'
  2. // Create your request types.
  3. const FETCH_STUFF = createRequest('FETCH_STUFF')
  4. // Have something async to run.
  5. const fetchStuff = () => fetch('https://unsplash.it/list').then(r => r.json())
  6. // Run it with Redux Saga.
  7. function* mySaga() {
  8. yield request(FETCH_STUFF, [fetchStuff])
  9. }
  10. // Do stuff easily in your reducers.
  11. const stuffReducer = (state = {}, { type, payload }) => {
  12. switch (type) {
  13. case FETCH_STUFF.STARTED:
  14. return {
  15. ...state,
  16. // Maybe use this to show a loading spinner?
  17. fetching: true,
  18. }
  19. case FETCH_STUFF.SUCCEEDED:
  20. return {
  21. ...state,
  22. fetching: false,
  23. fetched: true,
  24. // Here's the stuff!
  25. stuff: payload,
  26. }
  27. case FETCH_STUFF.CANCELLED:
  28. case FETCH_STUFF.ERRORED:
  29. return {
  30. ...state,
  31. fetching: false,
  32. fetched: false,
  33. // Oh no, show an error message based on this.
  34. errored: true,
  35. }
  36. default:
  37. return state
  38. }
  39. }

Advanced Usage

  • You can pass arguments to your async function.
  • You can attach metadata to all request actions to keep them
    associated.
  1. function* mySaga() {
  2. yield request(
  3. FETCH_STUFF,
  4. [fetchStuff, fetchStuffArg],
  5. { fetchStuffMeta: 'META' }
  6. )
  7. }