如何在redux框架中使用Action和Reducer记录响应时间

How to log response times with Action and Reducer in redux framework

本文关键字:Action Reducer 记录 响应时间 redux 框架      更新时间:2023-09-26

我计划记录API调用所用时间的时间戳。所以我使用下面的模块。

https://www.npmjs.com/package/timer-stopwatch

我正在记录它的动作文件,但我如何访问这个对象在我的减速器当调用完成?

console.log('response_time :' + stopwatch.ms);
return {
        type:    GET_SEARCH_DATA,
        promise: request.get(API_URL + qs)
    }

那么在我的减速器中我如何访问stopwatch对象呢?

export default function searchReducer(state = defaultState, action) {

     switch(action.type) {
        case GET_SEARCH_DATA:
          console.log('i want to log the response time here');
          return state;
        default:
          return state;
      }
    }

我不会重复这个答案,所以你最好直接看一下答案:

Redux:使用异步中间件与在成功函数上调度操作

在我以前的项目中,我也有我自己的中间件来调用API,所以我认为这是一个很好的解决方案来处理你自己的API调用和添加额外的操作。

您可以在中间件中记录时间戳,因此您可以只实现一次。