简体   繁体   English

我应该如何将每个调度调用记录到 Redux 存储?

[英]How should I log every dispatch call to a Redux store?

I want to debug my Redux application by logging every action sent to the dispatcher in my the console.我想通过在控制台中记录发送到调度程序的每个操作来调试我的 Redux 应用程序。

What is generally regarded as the best approach for doing this?通常认为这样做的最佳方法是什么?

You can use a simple logging middleware, like the example in the redux middleware docs :您可以使用简单的日志记录中间件,如redux 中间件文档中的示例:

const logger = store => next => action => {
  console.group(action.type)
  console.info('dispatching', action)
  let result = next(action)
  console.log('next state', store.getState())
  console.groupEnd()
  return result
}

Or use the redux-logger module, which basically does the same thing.或者使用redux-logger模块,它基本上做同样的事情。

With TypeScript, use this annotation (where TStore is the type-name of the root of your state: 使用 TypeScript,使用此注释(其中TStore是状态根的类型名称:

import { Middleware } from 'redux';

  // ...

const logger: Middleware<{},TState> = store => next => action => {
  console.group(action.type)
  // etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM