1

谁能告诉我在减速器函数上使用链和在redux-auto中的主索引减速器函数中工作有什么区别

我想保存一个错误,

A) 商店/聊天/send.js

import actions from 'redux-auto'

//...

 function rejected(chat, payload, error){
  return chat;
} onError.chain = (c, p, error) => actions.logger.save(error)

//...

或者

B) 存储/记录器/index.js

import actions from 'redux-auto'
import save from './save'

export default function (errorsLog = [], action)
{
   if(action.type == actions.chat.send.rejected){
      return save(errorsLog,action.payload)
   }
   return errorsLog
}

他们都工作

我的问题:

  1. 我不知道什么会更好。有什么区别?

  2. 为什么我要使用其中一个?

  3. logger.save(...)另外,我不能只 调用rejected. 为什么会有这个chain功能?

谢谢你的帮助 :)

4

2 回答 2

1

A) Using the chain(OnError) will fire the action AFTER the source(rejected) reducer has completed. creating a new call across you store.

B) You are changing the state in the source reducer call

Your qustions:

1,2) Using chaining will make you code more readable as the next function is collocated with the source reducer, but but having it in the index group all action that will happen to that part of the store.

3) Calling an action function directly within a reducer function. Is an anti-pattern. This is dispatching an action in the middle of a dispatched action. The reducer will be operating on inconsistent data.

于 2017-08-09T12:17:39.970 回答
0

Redux 的主要观点之一是可预测性。我们应该尽可能多地使用pure函数。减速器根本不能有任何副作用。

最近我研究了相同的功能——错误(用户操作等)日志记录。我认为所有这些行动都是side-effects。它们对用户没有利润,也不能成为主要业务逻辑的一部分。

这就是为什么我用它custom middleware来捕获我需要记录的所有操作。我需要记录的操作我已经用一些meta-prop(例如{log: 'errorLog'})标记了,并且在中间件中我检查了每个操作。如果它有一个log道具,那么我会制作一些记录器魔法。

最后,我得到了清晰易懂的代码,其中所有日志记录副作用都封装在中间件中。

于 2017-07-15T08:33:07.730 回答