博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react 中间件的应用
阅读量:6955 次
发布时间:2019-06-27

本文共 2768 字,大约阅读时间需要 9 分钟。

以arc-web项目为例子

1:http/index.js

这个文件主要的作用是 发起http请求,并拿到结果

发起http请求的部分

const invokeApi = (参数1-配置参数) => (参数2-URL等请求参数) => {发起URL请求,返回请求结果}

2:http/requestJson.js : 引入 http/index.js

这个文件的作用主要是提供参数1

3:index.js

在这一步主要是引入第二步的返回结果,作为作为中间的参数使用 reduxRequestMiddleware(httpRequestJson)

中间件的具体写法

export const httpApi = Symbol('call ajax api')// requestJson为第二步中的返回结果 即invokeApi函数  action这是操作步骤action :export function getProductOrderTotalList(queryParam) {  return {    [httpApi]: {      url: '/arc/report/order_summarize/list',      options: {        method: 'POST',        body: queryParam,      },      types: ['GET_PRODUCT_ORDER_TOTAL_LISTT_SUCCESS'],    },  }}const middleware = (requestJson, catchError) => store => next => (action) => {  const httpSymbol = action[httpApi]  // 拿到action中的httpApi  if (typeof httpSymbol === 'undefined') {// 如果找不到 httpApi,则不走中间件的流程,直接进行下一步,next(action)的作用就是执行下一步,即发起dispatch(action)    return next(action)  }  const { options, types, url, acceptType } = httpSymbol // 检验参数,例如URL和type等  if (typeof url !== 'string') {    throw new Error('请指定请求路径!')  }  if (!Array.isArray(types) || !types.every(x => typeof x === 'string')) {    throw new Error('请指定的action types数组,且每个元素为string类型')  }  const actionWith = obj => ({    ...action,    [httpApi]: undefined,    requestInformation: httpSymbol,    ...obj,  })  const [    successType,    requestType = 'HTTP_REQUEST',    failureType = 'HTTP_FAILURE',    errorType = 'HTTP_ERROR',    requestCompletedType = 'HTTP_REQUEST_COMPLETED',  ] = types// 发起一个action http请求开始  next(actionWith({ type: requestType }))// 如果发起的请求的类型为blob,则走下面这一步  if (acceptType === 'blob') {    return requestJson(url, options, acceptType)      .then(response => response.blob().then(blob => next(actionWith({        files: {          name: response.headers.get('filename'),          blob,        },        type: successType,      }))).catch(error => next(actionWith({        error,        type: errorType,      }))))      .catch(error => next(actionWith({        error,        type: errorType,      })));  }// requestJson是一个函数,为第一步的函数,是发起一个http请求,获取服务器资源  return requestJson(url, options).then((response) => {// 发起一个action 表示请求完成    next(actionWith({ type: requestCompletedType }))    if (response.resultCode === '000000') {// 如果服务器返回的结果是00000,表示返回成功 发起一个action type为successType,并且返回从服务器拿到的结果      return next(actionWith({        type: successType,        response,      }))    }// 如果失败,则发起一个action type为failureType    return next(actionWith({      response,      type: failureType,    }))  }).catch((error) => {    next(actionWith({      error,      type: errorType,    }))    if (typeof catchError === 'function') {      catchError(error)    }  })}export default middleware复制代码

转载于:https://juejin.im/post/5ad0169c518825557e78cb90

你可能感兴趣的文章
Linux虚拟机添加硬盘
查看>>
Unable to resolve module LinkedStateMixin
查看>>
Socket通信3——用Swing来构建电脑服务端界面
查看>>
pandas和numpy学习
查看>>
------第二节-----------------第二讲----单链表的基本操作---------
查看>>
查看LINUX进程内存占用情况
查看>>
在非activity类调用startActivityForResult
查看>>
Logstash使用grok过滤nginx日志
查看>>
Codeforces632E 选择/小偷与商店 背包DP
查看>>
5.索引简介
查看>>
重定位细节[1]
查看>>
索引器
查看>>
UVa 11044 - Searching for Nessy
查看>>
Linux Cluster
查看>>
JQ 移动端返回顶部,往下滑动时显示返回按钮,往上滑动时隐藏返回按钮
查看>>
购物车--楼层效果
查看>>
python 按位置关系输出矩阵元素
查看>>
C#取模的理解:为什么当a<b,a%b=a?
查看>>
动态规划本质理解:01背包问题
查看>>
面向对象原则之一 单一职责原则
查看>>