项目作者: cpv123

项目描述 :
Simple redux middleware to make data fetching consistent
高级语言: TypeScript
项目地址: git://github.com/cpv123/fetch-middleware.git
创建时间: 2019-08-25T13:26:10Z
项目社区:https://github.com/cpv123/fetch-middleware

开源协议:

下载


Simple redux middleware to make fetching data more consistent - each request will dispatch a START action, along with either a RECEIVE or ERROR.

Actions named with an @@ prefix will be handled by this middleware; an optional helper function is provided to ensure that the actions intended for this middleware are formatted correctly.

  1. // using the fetchAction helper function
  2. const getData = () => fetchAction('GET_DATA', fetch(url))
  3. // or manually formatting the action
  4. const getData = () => ({
  5. type: '@@GET_DATA',
  6. requestToMake: fetch(url),
  7. })
  8. export default connect(null, { getData })(Child)
  1. // example reducer for the above
  2. const actionHandlers = {
  3. 'GET_DATA_START': (state) => ({
  4. ...state,
  5. isLoading: true,
  6. }),
  7. 'GET_DATA_RECEIVE': (state, action) => ({
  8. ...state,
  9. isLoading: false,
  10. data: action.payload,
  11. }),
  12. 'GET_DATA_ERROR': (state) => ({
  13. ...state,
  14. isLoading: false,
  15. isError: true,
  16. }),
  17. }